Converting DIB to DDB

You can render a device-independent bitmap (DIB) onto a device without having to convert it into a device-dependent bitmap (DDB). However, the process is slower when using a DIB and you also don't have access to few very versatile functions such as BitBlt() which handle only DDBs.

The basic steps involved in creating a device-dependent bitmap from a device-independent bitmap are:

  1. Create a logical palette from the information in the DIB color table. You need to do this only if the device supports palettes. To create the palette, allocate enough memory for a LOGPALETTE structure with memory for the color table as well. Initialize the palVersion and palNumEntries fields and copy the color values from the color table in the DIB. Then call CreatePalette() using the LOGPALETTE structure we initialized.
  2. Select the logical palette into the device for which we are creating the DDB, then realize the palette.
  3. Create the DDB using the CreateDIBitmap() function.
  4. Don't forget to release any memory allocated for the LOGPALETTE structure.

Although this function creates a logical palette, it doesn't return this information to the calling code. If the DIB represents a 256 color bitmap and the device supports only 256 colors, then rendering the DDB onto the device will probably not result in the proper image. That's because the system palette colors may not match the colors that are being used by the bitmap. You may, therefore, want to modify the function so that it returns the logical palette as well. You would have to select and realize this palette onto the device context before drawing the bitmap image.

	HBITMAP 		hbm;
	CPalette		pal;
	CPalette*		pOldPal;
	CClientDC		dc(NULL);

	if (hDIB == NULL)
		return NULL;


	int nColors = lpbi->biClrUsed ? lpbi->biClrUsed : 
						1 << lpbi->biBitCount;

	if( bmInfo.bmiHeader.biBitCount > 8 )
		lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors + 
			bmInfo.bmiHeader.biClrUsed) + 
			((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
		lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);

	// Create and select a logical palette if needed
	if( nColors <= 256 && dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
		UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);

		pLP->palVersion = 0x300;
		pLP->palNumEntries = nColors;

		for( int i=0; i < nColors; i++)
			pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed;
			pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen;
			pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;
			pLP->palPalEntry[i].peFlags = 0;

		pal.CreatePalette( pLP );

		delete[] pLP;

		// Select and realize the palette
		pOldPal = dc.SelectPalette( &pal, FALSE );

	hbm = CreateDIBitmap(dc.GetSafeHdc(),		// handle to device context
			(LPBITMAPINFOHEADER)lpbi,	// pointer to bitmap info header 
			(LONG)CBM_INIT,			// initialization flag
			lpDIBBits,			// pointer to initialization data 
			(LPBITMAPINFO)lpbi,		// pointer to bitmap info
			DIB_RGB_COLORS );		// color-data usage 

	if (pal.GetSafeHandle())

	return hbm;


  • Problem with hdib parameter

    Posted by Legacy on 02/19/2004 12:00am

    Originally posted by: SteH

    Is it only me? When debugging this code the lpbi-> values are all rubbish since hdib as I have it is a double pointer. That means the line 
    lpbi = (LPBITMAPINFOHEADER)hdib;
    should read
    lpbi = (LPBITMAPINFOHEADER)(*hdib);

  • how to use API loadresource

    Posted by Legacy on 04/18/2001 12:00am

    Originally posted by: Nance

    from MSDN, i use the function "loadresource",but the return handle is always NULL, i can't understand why? Thanks

  • Converting DIB to DDB

    Posted by Legacy on 02/11/1999 12:00am

    Originally posted by: Ted Shaneyfelt

    hDIB is being used as a pointer, but it's declared as a HANDLE.

    lpbi = (LPBITMAPINFO)hDIB; // Wrong???

    Perhaps the function should be coded either to take a HANDLE
    to an object allocated by GlobalAlloc or the function should
    be declared to take a pointer, because that's what it expects.
    Here's how to declare it to take a pointer:

    HBITMAP DIBToDDB( void *pDIB )
    lpbi = (LPBITMAPINFO)pDIB; // Ok

    Here's how to declare it to take a handle:

    lpbi = (LPBITMAPINFO)GlobalLock(hDIB); // Ok
    GlobalUnlock( hDIB ); // Done with lpbi

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date