The SHOWDIB sample application provided in versions 3.0 and 3.1 of the Windows SDK, the WINCAP sample application available in the Microsoft Download Center, and the DIBVIEW sample application provided in version 3.1 of the Windows SDK and in the Professional Edition of Visual C++ each demonstrate the steps described in this article.
- Initialize the BITMAPINFOHEADER data structure.
- Set biSize to sizeof(BITMAPINFOHEADER).
- Set biWidth to the width of the bitmap.
- Set biHeight to the height of the bitmap.
- Set biPlanes to 1.
- Set biBitCount to the bitmap bmPlanes multiplied by bmBitsPixel. This value can contain only the values 1, 4, 8, or 24.
- Set biCompression to the desired format (usually BI_RGB).
- All other fields can be set to zero.
bmi.biSize = sizeof(BITMAPINFOHEADER);
bmi.biWidth = bm.bmWidth;
bmi.biHeight = bm.bmHeight;
bmi.biPlanes = 1;
bmi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
if (bmi.biBitCount <= 1)
bmi.biBitCount = 1;
else if (bmi.biBitCount <= 4)
bmi.biBitCount = 4;
else if (bmi.biBitCount <= 8)
bmi.biBitCount = 8;
bmi.biBitCount = 24;
bmi.biCompression = BI_RGB;
bmi.biSizeImage = 0;
bmi.biXPelsPerMeter = 0;
bmi.biYPelsPerMeter = 0;
bmi.biClrUsed = 0;
bmi.biClrImportant = 0;
- If there is a logical palette associated with the bitmap, select the palette into the desired device context using SelectObject(), then realize the palette with RealizePalette().
- Allocate a block of memory large enough to store the
BITMAPINFOHEADER structure and the color table for the DIB. The color table is an array of RGBQUAD structures that contain the colors used by the image. The size of the color table is the number of colors multiplied by sizeof(RGBQUAD). The number of colors is found using the biBitCount field in BITMAPINFOHEADER, which specifies the color resolution (bits per pixel). For example, 1 bit-per-pixel means monochrome (2 colors), 4 bits-per-pixel means 16 colors, and 8 bits-per-pixel means 256 colors. For a 24-bit color image, however, there is no color table.
- Call the GetDIBits() function with the desired device context (DC), a pointer to the memory block as lpBitsInfo, and NULL as the lpBits parameter. Doing so instructs the device driver to fill in the
biSizeImage field of the BITMAPINFOHEADER structure (this field should have been initialized to 0 in step 1). The biSizeImage field specifies the amount of memory required (in bytes) to store the bits that make up the image of the DIB. If this field remains 0 after calling GetDIBits(), the application must calculate biSizeImage using the following formula:
This formula is required because each scan line of the image is aligned on a DWORD boundary.
((((biWidth * biBitCount) + 31) & ~31) >> 3) * biHeight
- Reallocate the memory block containing the BITMAPINFOHEADER and color table to provide additional memory to store the bits of the DIB.
- Call GetDIBits() with the desired DC, a pointer to the memory block as lpBitsInfo, a pointer to the buffer for the bitmap bits as lpBits, and wUsage as DIB_RGB_COLORS. This stores the bits of the DIB with the BITMAPINFOHEADER structure and completes the color table.
NOTE: The first two samples convert bitmaps into Windows 3.0 style DIBs and do not support OS/2-style DIBs. DIBVIEW does support OS/2-style DIBs.
For more information on the Windows 3.0-style DIB format, see the documentation for the BITMAPINFO, BITMAPINFOHEADER, and RGBQUAD structures in the "Microsoft Windows Software Development Kit Reference Volume 2" version 3.0 manual and in the Windows 3.1 SDK "Programmer's Reference, Volume 3: Messages, Structures, and Macro's" manual.
To convert a DIB to a DDB, do the following:
- Create a logical palette from the information in the DIB color table. Complete a LOGPALETTE data structure using the color values found in the color table, then call CreatePalette().
- Select the logical palette into the desired device context using
SelectObject(), then realize the palette using RealizePalette().
- Call CreateDIBitmap() using the desired DC and the pointer to the DIB as the lpInfoHeader parameter. This function returns a handle to a DDB.
Article ID: 80080 - Last Review: Feb 11, 2005 - Revision: 1