BOOL Capture(HDC hDC, LPRECT lpRect, LPCWSTR lpszFile)
{
BITMAPINFO bmi = { 0 };
LPBYTE pBits = NULL;
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 = 32;
bmi.bmiHeader.biCompression = BI_RGB;
HBITMAP hBitmap = CreateDIBSection(hDC, &bmi,
DIB_RGB_COLORS, (LPVOID*)&pBits, NULL, 0);
HDC hMemDC = CreateCompatibleDC(hDC);
HGDIOBJ 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);
DWORD nImageSize = ((((bmi.bmiHeader.biWidth *
bmi.bmiHeader.biBitCount)
+ 31) & ~31) >> 3)
* bmi.bmiHeader.biHeight;
BITMAPFILEHEADER header = { 0 };
header.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) +
(DWORD)sizeof(BITMAPINFOHEADER);
header.bfSize = nImageSize + sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER);
header.bfType = 0x4D42;
HANDLE hFile = CreateFile(lpszFile, GENERIC_WRITE,
0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
DWORD nWritten = 0;;
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;
}
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;
}
#include <Windows.h>
BOOL Capture(HDC, LPRECT, LPCWSTR);
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
HDC hDC;
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = 260;
rect.bottom = 512;
hWnd = GetDesktopWindow();
hDC = GetDC(hWnd);
Capture(hDC, &rect, L"capture.bmp");
return 0;
}
Compiled this application will grab the top-left corner of the desktop.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)