Creating a DIB

The CreateDIBitmap function creates a device-independent bitmap. You can use this function in two different ways. The function call:

hBitmap = CreateDIBitmap (hdc, &bmih, 0L, NULL, NULL, 0) ;

creates an uninitialized bitmap. The second parameter is a pointer to an initialized BITMAPINFOHEADER structure. The function call:

hBitmap = CreateDIBitmap (hdc, &bmih, CBM_INIT,

lpBits, &bmi, wUsage) ;

creates an initialized bitmap. The lpBits parameter is a pointer to the array of bits. The fifth parameter is a pointer to an initialized BITMAPINFO structure.

The BITMAPINFO structure is defined as follows:

typedef structure tagBITMAPINFO

{

BITMAPINFOHEADER bmiHeader ;

RGBQUAD bmiColors[1] ;

}

BITMAPINFO ;

The first field is an initialized BITMAPINFOHEADER structure, and the second field is an array of initialized RGBQUAD structures that define the color table. Windows needs this color table when the bitmap is initialized to properly interpret the bitmap image data and to perform any color conversions required by the device.

Note that the RGBQUAD array contains only one element. To use this structure you must allocate a memory block equal in size to the BITMAPINFOHEADER structure plus enough RGBQUAD structures for the whole color table.

The wUsage parameter to CreateDIBitmap can be either DIB_RGB_COLORS (which means that the color table contains RGB color values) or DIB_PAL_COLORS (indicating that the color table is an array of 2-byte values that index a palette).

Two functions are available to set and obtain the bits of the bitmap. The first function sets the bits:

SetDIBits (hdc, hBitmap, nStart, nNum,

lpBits, &bmi, wUsage) ;

The last three parameters are the same as in the CreateDIBitmap function. The nStart parameter indicates the beginning scan line addressed by lpBits. This can range from 0 (for the bottom scan line) to the height of the bitmap in pixels minus 1 (for the top scan line). The nNum parameter indicates the number of scan lines to set into the bitmap.

The GetDIBits function has identical parameters:

GetDIBits (hdc, hBitmap, nStart, nNum,

lpBits, &bmi, wUsage) ;

But in this case lpBits points to a buffer to receive the bitmap bits. The function sets the fields of the BITMAPINFO structure to indicate the dimensions of the bitmap and the color table.

Like the old-style bitmap, a DIB can be deleted using DeleteObject.