Writing a window image to a BMP file


To save the image of a window to a BMP file, you have to draw the image to a compatible memory device context, convert the bitmap associated with the memory DC to a device-independent bitmap and then write the DIB to a file. In previous sections, we have already covered how to get a DIB from a device-dependent bitmap and how to save a DIB to a file. The WriteWindowToDIB() function uses the DDBToDIB() and the WriteDIB() functions to accomplish its task.

Let's focus on how we get the window's image onto the memory device context and how to create a logical palette that corresponds to the image. To begin with, we create a memory DC that is compatible with the window's DC. Next we create a compatible bitmap that is the same size as the window. We then use BitBlt() to transfer the image to the memory DC and that gives us the bitmap.

However, this is just half of the job; the other half is to get the logical palette that can be used with the bitmap. The bitmap won't display properly if it needs a logical palette and one is not available. Depending on the display settings we don't always need a logical palette. We use GetDeviceCaps() to determine whether a display device supports palette operation. For instance, if the display is set for true color then it doesn't support a palette. We allocate enough memory for a LOGPALETTE structure and then use the GetSystemPaletteEntries() to fill the color table. Now we can create the logical palette.

The function then uses the DDBToDIB() and the WriteDIB() functions to complete the job. You should note that if the window is partially or completely covered by other windows then the saved bitmap will show parts of these other windows too.
BOOL WriteWindowToDIB( LPTSTR szFile, CWnd *pWnd )
{
	CBitmap 	bitmap;
	CWindowDC	dc(pWnd);
	CDC 		memDC;
	CRect		rect;

	memDC.CreateCompatibleDC(&dc); 

	pWnd->GetWindowRect(rect);

	bitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height() );
	
	CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
	memDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc, 0, 0, SRCCOPY); 

	// Create logical palette if device support a palette
	CPalette pal;
	if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE )
	{
		UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
		LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
		pLP->palVersion = 0x300;

		pLP->palNumEntries = 
			GetSystemPaletteEntries( dc, 0, 255, pLP->palPalEntry );

		// Create the palette
		pal.CreatePalette( pLP );

		delete[] pLP;
	}

	memDC.SelectObject(pOldBitmap);

	// Convert the bitmap to a DIB
	HANDLE hDIB = DDBToDIB( bitmap, BI_RGB, &pal );

	if( hDIB == NULL )
		return FALSE;

	// Write it to file
	WriteDIB( szFile, hDIB );

	// Free the memory allocated by DDBToDIB for the DIB
	GlobalFree( hDIB );
	return TRUE;
}



Comments

  • There are no comments yet. Be the first to comment!

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

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds