One bit per pixel images are essentially an indexed format image having two entries in the colour palette. Unlike a GIF or other 256 colour indexed format which uses a single byte to access one of up-to 256 colours in the palette, the one bit per pixel format uses only a single bit to select colour A or colour B from the palette.
Often used in Fax formats, one bit per pixel images are very compact with one byte of information representing eight pixels.
Very often, a more complex RGB image can be used for the basic raw-image that is used to define the 1 bpp array but this operation in GDI+ and .NET systems is a little more complex than one would hope because you cannot simply get the Graphics object for an indexed image and draw on it. In order to do this conversion, you must manipulate the individual bits of the image by locking it's byte array and selecting the correct bit for the desired pixel.
The LockBits method can be used to obtain a BitmapData object which contains the address of the bitmap in memory, the dimensions and particularly, the stride of the image. All images are stored in an array that fills memory to a four-byte boundary so the stride is used to calculate where each successive line begins in the array. A specific line may be addressed by the formula LineAddress=stride*linenumber and NOT LineAddress = pixeldepth*imagewidth*linenumber as many people assume.
For our single bit per pixel image, the pixel indexing is further complicated by the need to select a specific bit in a byte that contains up to 8 pixels (it may have less than 8 pixels because it may be on the end of a scan-line). To perform this selection, a bit mask is used and rotated into position so that the correct bit may be set or reset as desired.
The simple application shown in listing 1 enables you to load any image into a PictureBox control situated on the left of the form. Once loaded, the image is scanned pixel by pixel to check the brightness of the colours contained in the image. If a pixel is darker than 50% brightness, a black pixel is set in the corresponding pixel of a black and white, single bit per pixel image stored in the rightmost PictureBox on the form. Between the two PictureBox controls, a Splitter enables you to see more or less of the original image for comparison with the converted one.
Note in particular how the single bit per pixel image is created in the pictureBox1_Click handler and how the SetIndexedPixel method is used to access the individual pixel bits in the monochrome image.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Syst

本文介绍如何将RGB图像转换为1位每像素的单色图像,这种格式常用于传真,非常紧凑,1字节信息代表8个像素。通过锁定图像的字节数组并选择每个像素的正确位来实现转换。使用LockBits方法获取BitmapData对象,然后通过SetIndexedPixel方法设置单色图像中每个像素的位。
最低0.47元/天 解锁文章
6641

被折叠的 条评论
为什么被折叠?



