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

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds