11.3.4 Displaying a Device-Independent Bitmap

One of the advantages of device-independent bitmaps is that you can display them directly without having to create an intermediate memory bitmap. The SetDIBitsToDevice function sets all or part of a device-independent bitmap directly to an output device, significantly reducing the memory required to display the bitmap. When you call SetDIBitsToDevice to display a bitmap, you must supply the following information:

The device context of the target output device

The location in the device context where the bitmap will appear

The size of the bitmap on the output device

The number of scan lines in the source-bitmap buffer from which you are copying the bitmap

The location of the first pixel in the source bitmap to copy to the output device

The device-independent bitmap-information structure and a buffer containing the bitmap to be displayed

Whether the color table of the DIB specification contains literal red, green, blue (RGB) color values or logical-palette color indices

Note:

The origin for device-independent bitmaps is the lower-left corner of the bitmap, not the upper-left corner as for other graphics operations.

Following is an example of how an application calls SetDIBitsToDevice:

SetDIBitsToDevice(hDC, 0, 0, lpbi->bmciHeader.bcWidth,
    lpbi->bmciHeader.bcHeight, 0, 0, 0,
    lpbi->bmciHeader.bcHeight,
    pBuf, (BITMAPINFO FAR*) lpbi,
    DIB_RGB_COLORS);

In this example, hDC identifies the device context of the target output device; SetDIBitsToDevice uses this information to identify the screen and determine the correct color format for the device bitmap.

The next two parameters specify the point on the display surface where SetDIBitsToDevice will begin drawing the bitmap; in this case, it is the origin of the device context itself. The next two parameters supply the width and height of the bitmap.

The sixth and seventh parameters, both of which are set to zero in this example, specify the first pixel in the source bitmap to be set on the display device; again, since both are zero, SetDIBitsToDevice begins with the first pixel in the bitmap buffer.

The next two parameters are used for banding purposes. The first of these two parameters is set to zero, indicating that the beginning scan line should be the first in the buffer; the second parameter is set to the height of the bitmap. As a result, the entire source bitmap will be set on the display surface in a single band.

The actual bitmap bits are contained in the pBuf buffer, and the lpbi parameter supplies the BITMAPINFO data structure that describes the color format of the source bitmap.

The last parameter is a usage flag that indicates whether the bitmap color table contains actual RGB color values or indices into the currently realized logical palette. DIB_RGB_COLORS specifies that the color table contains explicit color values.