Using Bitmaps

You can create a device-independent bitmap (DIB) with the CreateDIBSection function, and then select it into a device context with the SelectObject function. You use the DeleteObject function to delete the DIB.

In order to store a device-dependent bitmap in memory, you must first create a memory device context (DC) with the CreateCompatibleDC function. This function creates a DC that is compatible with the specified device. The DC contains a single-bit array that serves as a placeholder for a bitmap. You can use the CreateBitmap or CreateCompatibleBitmap function to create a bitmap of the desired size, and then select it into the DC with the SelectObject function. Windows CE then replaces the single-bit array with an array large enough to store color information for the specified rectangle of pixels.

When you draw using the handle returned by CreateCompatibleDC, the output does not appear on a device's drawing surface; instead, it is stored in memory. To copy the image stored in memory to a display device, call the BitBlt function. BitBlt copies the bitmap data from the bitmap in the source DC into the bitmap in the target DC. In this case, the source DC is the memory DC, and the target DC is the display DC. Thus, when BitBlt completes the transfer, the image will appear on the screen. By reversing the source and target DCs, you can use BitBlt to transfer images from the screen into memory.

Bit block transfer (BLT) functions, such as BitBlt, can be used to modify as well as transfer bitmaps. These functions modify a destination bitmap by combining it with a pen, a brush, and, in some cases, a source bitmap, in a format specified by a raster operation (ROP) code. Each ROP code specifies a unique logical pattern for combining graphic objects. For example, the SRCCOPY ROP simply copies a source bitmap to a destination bitmap while the MERGECOPY ROP merges the colors of a source rectangle with a specified pattern.

The ROP code types are described in the following table.

ROP type Description
ROP2 Combines a pen or brush with a destination bitmap in one of 16 possible combinations.
ROP3 Combines a brush, a source bitmap, and a destination bitmap in one of 256 possible combinations.
ROP4 Uses a monochrome "mask" bitmap to combine a foreground ROP3 and a background ROP3. The mask uses zeros and ones to indicate the areas where each ROP3 will be used.

When the source and destination bitmaps are different sizes, you can use the StretchBlt function to perform a BLT between the two bitmaps. StrechBlt copies a bitmap from a source rectangle into a destination rectangle, stretching or compressing the bitmap to fit the destination rectangle.

You can use the PatBlt function to paint a selected rectangle using a selected brush and an ROP3 code.

You can use the TransparentImage to transfer all portions of a bitmap except for those drawn in a specified transparent color. This function is especially useful for transferring non-rectangular images, such as icons.

Note Windows CE supports arbitrary bit pixel formats, which allow you to use BLT functions between bitmaps with different pixel depths.

The BITMAPINFO structure defines the dimensions and color information for a DIB. The BITMAPINFO structure must include a color table if the images are palettized, usually with 1-, 2-, 4-, and 8-bbp formats. For 16-bpp or-32-bpp non-palettized images, the color table must be three entries long; the entries must specify the value of the red, green, and blue bitmasks. Because GDI ignores the color table for 24-bpp bitmaps, you should store the image's pixels in Blue-Green-Red (BGR) format.