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 …

  • Some of the statistics about the growth of enterprise mobility are surprising, not the least of which is the 4X increase in mobile malware from 2013 to 2014. Many employees today not only expect to be able to use their own personal devices at work, but will even contravene policies that restrict personal device use for work.  IT must find the right approach that balances the benefits of mobility with the risks involved.  This on-demand webcast offers steps toward developing a winning mobile strategy, and …

Most Popular Programming Stories

More for Developers

RSS Feeds