MFC/Bitmap
Writing a window image to a BMP file
곰돌이짱
2013. 8. 8. 17:56
- 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;
- }