What is the easiest way to convert and save a 32-bit Bitmap to a 1-bit (black/white) .bmp file in C#?

This code will get the job done

using System.Drawing.Imaging;
using System.Runtime.InteropServices;
... public static Bitmap BitmapTo1Bpp(Bitmap img) {
int w = img.Width;
int h = img.Height;
Bitmap bmp = new Bitmap(w, h, PixelFormat.Format1bppIndexed);
BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
byte[] scan = new byte[(w + 7) / 8];
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if (x % 8 == 0) scan[x / 8] = 0;
Color c = img.GetPixel(x, y);
if (c.GetBrightness() >= 0.5) scan[x / 8] |= (byte)(0x80 >> (x % 8));
}
Marshal.Copy(scan, 0, (IntPtr)((long)data.Scan0 + data.Stride * y), scan.Length);
}
bmp.UnlockBits(data);
return bmp;
}

  

You can speed it up, if necessary, by using unsafe code to replace the GetPixel() method.

Save a 32-bit Bitmap as 1-bit .bmp file in C#的更多相关文章

  1. 32 位bitmap 内存存储 顺序 bgra 前3位 与23位一致。 都是 bgr 呵呵 与rgb 相反

    32 位bitmap     内存存储 顺序   bgra       前3位 与23位一致.   都是 bgr  呵呵 与rgb 相反

  2. Bitmap文件格式+生成一个BMP文件

    Bitmap的文件格式: #define UINT16 unsigned short #define DWORD unsigned int #define WORD short #define LON ...

  3. Photoshop做32位带Alpha通道的bmp图片

    原文链接: http://blog.sina.com.cn/s/blog_65c0cae801016e5u.html   批量制作32位带Alpha通道的bmp图片,可以制作一个动作,内容可以如下: ...

  4. C# Bitmap Save Generic GDI+ Error

    Image.Save 方法 (String) 将该 Image 保存到指定的文件或流. 命名空间:  System.Drawing程序集:  System.Drawing(在 System.Drawi ...

  5. Hide Data into bitmap with ARGB8888 format

    将保存重要信息,如银行卡密码的文本文件隐藏到ARGB8888的A通道. bitmap.h #ifndef BMP_H #define BMP_H #include <fstream> #i ...

  6. View转换为Bitmap及getDrawingCache

    View组件显示的内容可以通过cache机制保存为bitmap, 使用到的api有 void  setDrawingCacheEnabled(boolean flag),    Bitmap  get ...

  7. Bitmap图片的处理

      一.View转换为Bitmap 在Android中所有的控件都是View的直接子类或者间接子类,通过它们可以组成丰富的UI界面.在窗口显示的时候Android会把这些控件都加载到内存中,形成一个以 ...

  8. Android图片缓存之Bitmap详解

    前言: 最近准备研究一下图片缓存框架,基于这个想法觉得还是先了解有关图片缓存的基础知识,今天重点学习一下Bitmap.BitmapFactory这两个类. 图片缓存相关博客地址: Android图片缓 ...

  9. Bitmap工具类

    一直在使用的一个Bitmap工具类 处理Bitmap和ImageView对象,实现了下面功能: 1.saveBitmap: 把Bitmap对象持久存储到SD卡或手机内存. 2.getViewBitma ...

随机推荐

  1. HTML5 Differences from HTML4

    Abstract "HTML5 Differences from HTML4" describes the differences of the HTML5 specificati ...

  2. C语言基础 - read()函数读取文本字节导致判断失误的问题

    工作了几个月,闲着没事又拿起了经典的C程序设计看了起来,看到字符计数一节时想到用read()去读文本作为字符输入,一切OK,直到行计数时问题出现 了,字符总计数没有问题,可行计算就是进行不了,思考了半 ...

  3. 如何验证一个地址可否使用—— MmIsAddressValid函数分析

    又是一篇内核函数分析的博文,我个人觉得Windows的内核是最好的老师,当你想实现一个功能之前可以看看Windows内核是怎么做的,说不定就有灵感呢:) 首先看下官方的注释说明: /*++ Routi ...

  4. sicily 1046. Plane Spotting(排序求topN)

    DescriptionCraig is fond of planes. Making photographs of planes forms a major part of his daily lif ...

  5. tidb 记录文档

    ansible-playbook stop.yml   / start.yml 重启集群,在ansible目录下执行 SHOW STATS_META; 查看统计信息 重启集群:ansible-play ...

  6. 面试题41:和为s的两个数字 || 和为s的连续正数序列

    和为s的两个数字 题目:输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s.如果有多对数字的和等于s,输出任意一对即可. 有点类似于夹逼的思想 注意两个int相加的和要用lo ...

  7. Python 学习之list和Tuple类型

    1.创建list L = ['Adam', 95.5, 'Lisa', 85, 'Bart', 59] print(L) print(L[1],L[3],L[5])#索引 不能越界 正向访问 #95. ...

  8. Ionic Js十三:平台

    $ionicPlatform $ionicPlatform 用来检测当前的平台,以及诸如在PhoneGap/Cordova中覆盖Android后退按钮.  

  9. [CodeForces - 678F] Lena and Queries 线段树维护凸包

    大致题意: 给出三种操作 1.往平面点集中添加一个点 2.删除第i次添加的点 3.给出一个q,询问平面点集中的q*x+y的最大值 首先对于每个询问,可将z=q*x+y转化为y=z-q*x,即过点(x, ...

  10. 20172301 《Java软件结构与数据结构》实验一报告

    20172301 <Java软件结构与数据结构>实验一报告 课程:<Java软件结构与数据结构> 班级: 1723 姓名: 郭恺 学号:20172301 实验教师:王志强老师 ...