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 b