1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| BOOL Capture(HDC hDC, LPRECT lpRect, LPCWSTR lpszFile)
{
HDC hMemDC;
HBITMAP hBitmap;
HGDIOBJ hOld;
BITMAPINFO bmi;
LPBYTE pBits;
DWORD nImageSize;
DWORD nWritten;
BITMAPFILEHEADER header;
HANDLE hFile;
ZeroMemory((LPVOID)&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = lpRect->right - lpRect->left;
bmi.bmiHeader.biHeight = lpRect->bottom - lpRect->top;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
pBits = NULL;
hBitmap = CreateDIBSection(hDC, &bmi, DIB_RGB_COLORS, (LPVOID*)&pBits, NULL, 0);
hMemDC = CreateCompatibleDC(hDC);
hOld = SelectObject(hMemDC, hBitmap);
BitBlt(hMemDC, 0, 0, lpRect->right - lpRect->left, lpRect->bottom - lpRect->top, hDC, lpRect->left, lpRect->top, SRCCOPY);
SelectObject(hMemDC, hOld);
DeleteDC(hMemDC);
nImageSize = ((((bmi.bmiHeader.biWidth * bmi.bmiHeader.biBitCount) + 31) & ~31) >> 3) * bmi.bmiHeader.biHeight;
ZeroMemory((LPVOID)&header, sizeof(BITMAPFILEHEADER));
header.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
header.bfSize = nImageSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
header.bfType = 0x4D42;
hFile = CreateFile(lpszFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
WriteFile(hFile, (LPVOID)&header, sizeof(BITMAPFILEHEADER), &nWritten, NULL);
WriteFile(hFile, (LPVOID)&bmi, sizeof(BITMAPINFOHEADER), &nWritten, NULL);
WriteFile(hFile, (LPVOID)pBits, nImageSize, &nWritten, NULL);
CloseHandle(hFile);
}
return TRUE;
} |
Partager