HBITMAP CopyDCToBitmap(HDC hDC, LPRECT lpRect)
{
if(!hDC || !lpRect || IsRectEmpty(lpRect))
return NULL;
HDC hMemDC;
HBITMAP hBitmap, hOldBitmap;
int nX, nY, nX2, nY2;
int nWidth, nHeight;
nX = lpRect->left;
nY = lpRect->top;
nX2 = lpRect->right;
nY2 = lpRect->bottom;
nWidth = nX2 - nX;
nHeight = nY2 - nY;
hMemDC = CreateCompatibleDC(hDC);
hBitmap = CreateCompatibleBitmap(hDC, nWidth, nHeight);
hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
StretchBlt(hMemDC, 0, 0, nWidth, nHeight, hDC, nX, nY, nWidth, nHeight, SRCCOPY);
hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);
DeleteDC(hMemDC);
DeleteObject(hOldBitmap);
return hBitmap;
}
BOOL SaveBmp(HBITMAP hBitmap, const char *pchFileName)
{
if(!hBitmap || !pchFileName)
return FALSE;
HDC hDC;
int iBits; // µ±Ç°·Ö±æÂÊÏÂÿÏóËØËùÕ¼×Ö½ÚÊý
WORD wBitCount; // λͼÖÐÿÏóËØËùÕ¼×Ö½ÚÊý
DWORD dwPaletteSize=0, dwBmBitsSize=0, dwDIBSize=0, dwWritten=0;
BITMAP Bitmap; // λͼÊôÐԽṹ
BITMAPFILEHEADER bmfHdr; // λͼÎļþÍ·½á¹¹
BITMAPINFOHEADER bi; // λͼÐÅϢͷ½á¹¹
LPBITMAPINFOHEADER lpbi; // Ö¸ÏòλͼÐÅϢͷ½á¹¹
HANDLE fh, hDib, hPal, hOldPal = NULL; // ¶¨ÒåÎļþ£¬·ÖÅäÄÚ´æ¾ä±ú£¬µ÷É«°å¾ä±ú
hDC = CreateDC("DISPLAY", NULL, NULL, NULL);
iBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
DeleteDC(hDC);
if (iBits <= 1)
wBitCount = 1;
else if (iBits <= 4)
wBitCount = 4;
else if (iBits <= 8)
wBitCount = 8;
else
wBitCount = 24;
GetObject(hBitmap, sizeof(Bitmap), (LPSTR)&Bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = Bitmap.bmWidth;
bi.biHeight = Bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wBitCount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrImportant = 0;
bi.biClrUsed = 0;
dwBmBitsSize = ((Bitmap.bmWidth * wBitCount + 31) / 32) * 4 * Bitmap.bmHeight;
//ΪλͼÄÚÈÝ·ÖÅäÄÚ´æ
hDib = GlobalAlloc(GHND,dwBmBitsSize + dwPaletteSize + sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
*lpbi = bi;
// ´¦Àíµ÷É«°å
hPal = GetStockObject(DEFAULT_PALETTE);
if (hPal) {
hDC = ::GetDC(NULL);
hOldPal = ::SelectPalette(hDC, (HPALETTE)hPal, FALSE);
RealizePalette(hDC);
}
// »ñÈ¡¸Ãµ÷É«°åÏÂеÄÏñËØÖµ
GetDIBits(hDC, hBitmap, 0, (UINT) Bitmap.bmHeight,
(LPSTR)lpbi + sizeof(BITMAPINFOHEADER) + dwPaletteSize,
(BITMAPINFO *)lpbi, DIB_RGB_COLORS);
// »Ö¸´µ÷É«°å
if(hOldPal) {
::SelectPalette(hDC, (HPALETTE)hOldPal, TRUE);
RealizePalette(hDC);
::ReleaseDC(NULL, hDC);
}
// ´´½¨Î»Í¼Îļþ
fh = CreateFile(pchFileName, GENERIC_WRITE,0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh == INVALID_HANDLE_VALUE)
return FALSE;
// ÉèÖÃλͼÎļþÍ·
bmfHdr.bfType = 0x4D42; // "BM"
dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
+ dwPaletteSize + dwBmBitsSize;
bmfHdr.bfSize = dwDIBSize;
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER)
+ (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize;
// дÈëλͼÎļþÍ·
WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
// дÈëλͼÎļþÆäÓàÄÚÈÝ
WriteFile(fh, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL);
// Çå³ý
GlobalUnlock(hDib);
GlobalFree(hDib);
CloseHandle(fh);
return TRUE;
}