Save a 32-bit Bitmap as 1-bit .bmp file in C#
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#的更多相关文章
- 32 位bitmap 内存存储 顺序 bgra 前3位 与23位一致。 都是 bgr 呵呵 与rgb 相反
32 位bitmap 内存存储 顺序 bgra 前3位 与23位一致. 都是 bgr 呵呵 与rgb 相反
- Bitmap文件格式+生成一个BMP文件
Bitmap的文件格式: #define UINT16 unsigned short #define DWORD unsigned int #define WORD short #define LON ...
- Photoshop做32位带Alpha通道的bmp图片
原文链接: http://blog.sina.com.cn/s/blog_65c0cae801016e5u.html 批量制作32位带Alpha通道的bmp图片,可以制作一个动作,内容可以如下: ...
- C# Bitmap Save Generic GDI+ Error
Image.Save 方法 (String) 将该 Image 保存到指定的文件或流. 命名空间: System.Drawing程序集: System.Drawing(在 System.Drawi ...
- Hide Data into bitmap with ARGB8888 format
将保存重要信息,如银行卡密码的文本文件隐藏到ARGB8888的A通道. bitmap.h #ifndef BMP_H #define BMP_H #include <fstream> #i ...
- View转换为Bitmap及getDrawingCache
View组件显示的内容可以通过cache机制保存为bitmap, 使用到的api有 void setDrawingCacheEnabled(boolean flag), Bitmap get ...
- Bitmap图片的处理
一.View转换为Bitmap 在Android中所有的控件都是View的直接子类或者间接子类,通过它们可以组成丰富的UI界面.在窗口显示的时候Android会把这些控件都加载到内存中,形成一个以 ...
- Android图片缓存之Bitmap详解
前言: 最近准备研究一下图片缓存框架,基于这个想法觉得还是先了解有关图片缓存的基础知识,今天重点学习一下Bitmap.BitmapFactory这两个类. 图片缓存相关博客地址: Android图片缓 ...
- Bitmap工具类
一直在使用的一个Bitmap工具类 处理Bitmap和ImageView对象,实现了下面功能: 1.saveBitmap: 把Bitmap对象持久存储到SD卡或手机内存. 2.getViewBitma ...
随机推荐
- Introduction to MWB Minor Mode
Introduction to MWB Minor Mode */--> Table of Contents 1. Introduction 2. Usage 1 Introduction MW ...
- 打印数据的字节(十六进制)表示-c语言代码
先取数据地址,转换成单字节长度的类型(unsigned char)的指针,然后按照十六进制逐字节打印即可,格式为“%.2x”. sizeof()函数获取数据的字节数. /* $begin show-b ...
- 使用CSS3 @media 设置页面自适应
参考CSS3 @media 查询 如果文档宽度小于 300 像素则修改背景演示(background-color): @media screen and (max-width: 300px) { bo ...
- day5作业购物商城+ATM
模拟实现一个ATM + 购物商城程序 1.额度 15000或自定义 2.实现购物商城,买东西加入购物车,调用信用卡接口结账 3.可以提现,手续费5% 4.每月22号出账单,每月10号为还款日,过期未还 ...
- USACO 6.1 A Rectangular Barn
A Rectangular Barn Mircea Pasoi -- 2003 Ever the capitalist, Farmer John wants to extend his milking ...
- docker动态绑定端口
一.背景 在创建容器的时候,我们可以使用命令 docker container run -p host:container container-name 的方式来绑定端口,还可以使用docker-co ...
- 喜大普奔!Django官方文档终于出中文版了
喜大普奔!Django官方文档终于出中文版了 文章来源:企鹅号 - Crossin的编程教室 昨天经 Sur 同学告知才发现,Django 官方文档居然支持中文了! 之所以让我觉得惊喜与意外,是因为: ...
- MYSQL插入不能中文的问题的解决
这个问题是由于数据库的字符集不对的问题. 解决方法: 打开要用的数据库,输入命令 status 如果Client characterset 值为utf8,则要改为:set char set 'gbk' ...
- http远程调用原生get、post模板
一.get方法 package lq.httpclient.method; import java.io.BufferedReader; import java.io.IOException; imp ...
- python 自带的range是不能实现对小数的操作的,如果要对小数操作可以使用numpy
import numpy as np s = np.arange(0, 1, 0.1) print s [0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]