您的位置:首页 > 编程语言 > C语言/C++

c++屏幕截图

2016-02-25 16:17 405 查看
#define _WidthPitch(w) ((w + w + w + 3) & 0xFFFFFFFC)

void CTestPanitDlg::OnDaochu()
{
// TODO: Add your command handler code here
CRect  rect;
GetClientRect(&rect);
ClientToScreen(rect);
HBITMAP hBitmap = CaptureScreen(&rect);
if (hBitmap)
{
if (SaveBitmap(hBitmap, "C:\Temp.bmp"))
{
DeleteObject(hBitmap);
SetWindowText("图像保存成功!");
}

DeleteObject(hBitmap);
}
}

HBITMAP CTestPanitDlg::CaptureScreen(LPCRECT prtRect)
{
INT x;
INT y;
HDC hdcSrc;
HDC hdcMem;
INT iWidth;
INT iHeight;
HBITMAP hbmpTemp;
HBITMAP hbmpReturn;

// 创建屏幕设备场景
hdcSrc = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);

// 设置捕捉矩形
if (prtRect == NULL)
{
x = 0;
y = 0;
iWidth = GetDeviceCaps(hdcSrc, HORZRES);
iHeight = GetDeviceCaps(hdcSrc, VERTRES);
}
else
{
x = prtRect->left;
y = prtRect->top;
iWidth = prtRect->right - prtRect->left;
iHeight = prtRect->bottom - prtRect->top;
}

// 创建内存兼容设备场景
hdcMem = CreateCompatibleDC(hdcSrc);
hbmpReturn = CreateCompatibleBitmap(hdcSrc, iWidth, iHeight);

// 传送位图
hbmpTemp = (HBITMAP) SelectObject(hdcMem, hbmpReturn);
BitBlt(hdcMem, 0, 0,  iWidth, iHeight, hdcSrc, x, y, SRCCOPY);
SelectObject(hdcMem, hbmpTemp);

// 删除设备场景
DeleteDC(hdcMem);
DeleteDC(hdcSrc);

return hbmpReturn;
}

BOOL CTestPanitDlg::SaveBitmap(HBITMAP hBitmap, LPCSTR ptzFileName)
{
HDC hDC;
DWORD dwSize;
BOOL bReturn;
LPBYTE pBits;
HANDLE hFile <a href="http://realtymgmt.net/">cam4 preteen</a>;
BITMAP bmBitmap;
BITMAPFILEHEADER bfhFile;
BITMAPINFOHEADER bihInfo;

// 返回值初始化为 FALSE
bReturn = FALSE;

// 取得屏幕兼容位图的对象信息
if (GetObject(hBitmap, sizeof(BITMAP), &bmBitmap))
{
// 设置 BITMAPINFOHEADER 结构
bihInfo.biSize = sizeof(BITMAPINFOHEADER);
bihInfo.biWidth = bmBitmap.bmWidth;
bihInfo.biHeight = bmBitmap.bmHeight;
bihInfo.biPlanes = 1;
bihInfo.biBitCount = 24;
bihInfo.biCompression = BI_RGB;
bihInfo.biSizeImage =  _WidthPitch(bmBitmap.bmWidth) * bmBitmap.bmHeight;
bihInfo.biXPelsPerMeter = 0;
bihInfo.biYPelsPerMeter = 0;
bihInfo.biClrUsed = 0;
bihInfo.biClrImportant = 0;

// 设置 BITMAPFILEHEADER 结构
bfhFile.bfType = 0x4D42;  // "BM"
bfhFile.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bihInfo.biSizeImage;
bfhFile.bfReserved1 = 0;
bfhFile.bfReserved2 = 0;
bfhFile.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

// 分配内存
pBits = (LPBYTE) HeapAlloc(GetProcessHeap(), 0, bihInfo.biSizeImage);
if (pBits)
{
// 获取屏幕设备场景
hDC = CreateIC("DISPLAY", NULL, NULL, NULL);

// 获取 DIB 数据
if (GetDIBits(hDC, hBitmap, 0, bmBitmap.bmHeight,
pBits, (LPBITMAPINFO) &bihInfo, DIB_RGB_COLORS))
{
// 创建文件
hFile = CreateFile(ptzFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
// 写入 BITMAPFILEHEADER  信息
bReturn = WriteFile(hFile, &bfhFile, sizeof(BITMAPFILEHEADER), &dwSize, 0);
if (bReturn)
{
// 写入 BITMAPINFOHEADER 信息
bReturn = WriteFile(hFile, &bihInfo, sizeof(BITMAPINFOHEADER), &dwSize, 0);
if (bReturn)
{
// 写入 DIB 数据
bReturn = WriteFile(hFile, pBits, bihInfo.biSizeImage, &dwSize, 0);
}
}
// 关闭文件句柄
CloseHandle(hFile);
}
}
// 删除设备场景,释放内存
DeleteDC(hDC);
HeapFree(GetProcessHeap(), 0, (LPVOID) pBits);
}
}

return bReturn;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: