[Cpp]
HBITMAP CopyScreenToBitmap (LPRECT lpRect)
{
// Screen and memory device description table
HDC hSrcDC, hMemDC;
// Bitmap handle
HBITMAP hBitmap, hOldBitmap;
// Coordinates of the selected region
Int nX, nY, nX2, nY2;
// Bitmap width and height
Int nWidth, nHeight;
// Screen resolution
Int xScrn, yScrn;
// Make sure that the selected area is not empty.
If (IsRectEmpty (lpRect ))
Return NULL;
// Create a device description table for the screen
HSrcDC = CreateDC ("DISPLAY", NULL );
// Create a compatible memory device description table for the screen device description table
HMemDC = CreateCompatibleDC (hSrcDC );
// Obtain the coordinates of the selected region
NX = lpRect-> left;
NY = lpRect-> top;
NX2 = lpRect-> right;
NY2 = lpRect-> bottom;
// Obtain the screen resolution
XScrn = GetDeviceCaps (hSrcDC, HORZRES );
YScrn = GetDeviceCaps (hSrcDC, VERTRES );
// Make sure that the selected area is visible
If (nX <0)
NX = 0;
If (nY <0)
NY = 0;
If (nX2> xScrn)
NX2 = xScrn;
If (nY2> yScrn)
NY2 = yScrn;
NWidth = nX2-nX;
NHeight = nY2-nY;
// Create a bitmap compatible with the on-screen device description table
HBitmap = CreateCompatibleBitmap (hSrcDC, nWidth, nHeight );
// Select the new bitmap to the memory device description table.
HOldBitmap = (HBITMAP) SelectObject (hMemDC, hBitmap );
// Copy the screen device description table to the memory device description table.
BitBlt (hMemDC, 0, 0, nWidth, nHeight, hSrcDC, nX, nY, SRCCOPY );
// Obtain the handle of the screen bitmap.
HBitmap = (HBITMAP) SelectObject (hMemDC, hOldBitmap );
// Clear
DeleteDC (hSrcDC );
DeleteDC (hMemDC );
// Return location handle
Return hBitmap;
}
HBITMAP CopyScreenToBitmap (int x1, int x2, int y1, int y2)
{
// Screen and memory device description table
HDC hSrcDC, hMemDC;
// Bitmap handle
HBITMAP hBitmap, hOldBitmap;
// Coordinates of the selected region
Int nX, nY, nX2, nY2;
// Bitmap width and height
Int nWidth, nHeight;
// Screen resolution
Int xScrn, yScrn;
/*
// Make sure that the selected area is not empty.
If (IsRectEmpty (lpRect ))
Return NULL ;*/
// Create a device description table for the screen
HSrcDC = CreateDC ("DISPLAY", NULL );
// Create a compatible memory device description table for the screen device description table
HMemDC = CreateCompatibleDC (hSrcDC );
// Obtain the coordinates of the selected region
NX = x1;
NY = y1;
NX2 = x2;
NY2 = y2;
// Obtain the screen resolution
XScrn = GetDeviceCaps (hSrcDC, HORZRES );
YScrn = GetDeviceCaps (hSrcDC, VERTRES );
// Make sure that the selected area is visible
If (nX <0)
NX = 0;
If (nY <0)
NY = 0;
If (nX2> xScrn)
NX2 = xScrn;
If (nY2> yScrn)
NY2 = yScrn;
NWidth = nX2-nX;
NHeight = nY2-nY;
// Create a bitmap compatible with the on-screen device description table
HBitmap = CreateCompatibleBitmap (hSrcDC, nWidth, nHeight );
// Select the new bitmap to the memory device description table.
HOldBitmap = (HBITMAP) SelectObject (hMemDC, hBitmap );
// Copy the screen device description table to the memory device description table.
BitBlt (hMemDC, 0, 0, nWidth, nHeight, hSrcDC, nX, nY, SRCCOPY );
// Obtain the handle of the screen bitmap.
HBitmap = (HBITMAP) SelectObject (hMemDC, hOldBitmap );
// Clear
DeleteDC (hSrcDC );
DeleteDC (hMemDC );
// Return location handle
Return hBitmap;
}
BOOL SaveCapturedBitmap (HBITMAP hBitmap, CString FileName)
{
If (hBitmap = NULL | FileName. IsEmpty ())
{
AfxMessageBox ("parameter error ");
Return false;
}
HDC hDC;
// The number of bytes per pixel in the current resolution
Int iBits;
// The number of bytes in the bitmap.
WORD wBitCount;
// Defines the color palette size, pixel bytes in the bitmap, bitmap file size, and number of written file bytes.
DWORD dwPaletteSize = 0, dwBmBitsSize = 0, dwDIBSize = 0, dwWritten = 0;
// Bitmap attribute Structure
BITMAP Bitmap;
// Bitmap file header Structure
BITMAPFILEHEADER bmfHdr;
// Bitmap header Structure
BITMAPINFOHEADER bi;
// Point to the bitmap information header Structure
LPBITMAPINFOHEADER lpbi;
// Defines the file, allocates memory handles, and color palette handles.
HANDLE fh, hDib, hPal, hOldPal = NULL;
// Calculate the number of bytes occupied by each pixel in a bitmap file
HDC = CreateDC ("DISPLAY", 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;
// Allocate memory for bitmap content
HDib = GlobalAlloc (GHND, dwBmBitsSize + dwPaletteSize + sizeof (BITMAPINFOHEADER ));
Lpbi = (LPBITMAPINFOHEADER) GlobalLock (hDib );
* Lpbi = bi;
// Process the palette
HPal = GetStockObject (DEFAULT_PALETTE );
If (hPal)
{
HDC =: GetDC (NULL );
HOldPal =: SelectPalette (hDC, (HPALETTE) hPal, FALSE );
RealizePalette (hDC );
}
// Obtain the new pixel value under the color palette
GetDIBits (hDC, hBitmap, 0, (UINT) Bitmap. bmHeight, (LPSTR) lpbi + sizeof (BITMAPINFOHEADER)
+ DwPaletteSize, (BITMAPINFO *) lpbi, DIB_RGB_COLORS );
// Restore the palette
If (hOldPal)
{
: SelectPalette (hDC, (HPALETTE) hOldPal, TRUE );
RealizePalette (hDC );
: ReleaseDC (NULL, hDC );
}
// Create a bitmap file
Fh = CreateFile (FileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL );
If (fh = INVALID_HANDLE_VALUE) return FALSE;
// Set the bitmap file header
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;
// Write the bitmap file header
WriteFile (fh, (LPSTR) & bmfHdr, sizeof (BITMAPFILEHEADER), & dwWritten, NULL );
// Write the remaining contents of the bitmap file
WriteFile (fh, (LPSTR) lpbi, dwDIBSize, & dwWritten, NULL );
// Clear
GlobalUnlock (hDib );
GlobalFree (hDib );
CloseHandle (fh );
Return TRUE;
}