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

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds