VC获取窗口图片(截图)
2012-05-03 11:28
204 查看
//借鉴网上的文章:
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 bmp,char* path)
{
//把位图的信息保存到bmpinfo;
BITMAP bmpinfo;
GetObject(bmp,sizeof(BITMAP),&bmpinfo);
DWORD dwBmBitsSize = ((bmpinfo.bmWidth * 32+31)/32) * 4 * bmpinfo.bmHeight;
//位图文件头 14字节
BITMAPFILEHEADER bf;
bf.bfType = 0x4D42;
//BM
bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwBmBitsSize;
bf.bfReserved1 = 0;
bf.bfReserved2 = 0;
bf.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
//位图信息头
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bmpinfo.bmWidth;
bi.biHeight = bmpinfo.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 8;
bi.biClrImportant = 0;
//位图数据;
char* context = new char[dwBmBitsSize];
HDC dc = ::GetDC(NULL);
GetDIBits(dc, bmp, 0, bi.biHeight, context, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
FILE* f = fopen(path,"wb");
fwrite((char*)&bf,sizeof(BITMAPFILEHEADER),1,f);
fwrite((char*)&bi,sizeof(BITMAPINFOHEADER),1,f);
fwrite(context,dwBmBitsSize,1,f);
fclose(f);
delete context;
::ReleaseDC(NULL,dc);
return 0;
}
void CaptureScreen()
{
int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
HWND hDesktopWnd = GetDesktopWindow();
HDC hDesktopDC = GetDC(hDesktopWnd);
HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,
nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC,hCaptureBitmap);
BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,hDesktopDC,0,0,SRCCOPY);
SaveBmp(hCaptureBitmap, "F:\\capture.bmp"); //Place holder - Put your code
//here to save the captured image to disk
ReleaseDC(hDesktopWnd,hDesktopDC);
DeleteDC(hCaptureDC);
DeleteObject(hCaptureBitmap);
}
void CCaptureScreenDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CaptureScreen();
}
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 bmp,char* path)
{
//把位图的信息保存到bmpinfo;
BITMAP bmpinfo;
GetObject(bmp,sizeof(BITMAP),&bmpinfo);
DWORD dwBmBitsSize = ((bmpinfo.bmWidth * 32+31)/32) * 4 * bmpinfo.bmHeight;
//位图文件头 14字节
BITMAPFILEHEADER bf;
bf.bfType = 0x4D42;
//BM
bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwBmBitsSize;
bf.bfReserved1 = 0;
bf.bfReserved2 = 0;
bf.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
//位图信息头
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bmpinfo.bmWidth;
bi.biHeight = bmpinfo.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 8;
bi.biClrImportant = 0;
//位图数据;
char* context = new char[dwBmBitsSize];
HDC dc = ::GetDC(NULL);
GetDIBits(dc, bmp, 0, bi.biHeight, context, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
FILE* f = fopen(path,"wb");
fwrite((char*)&bf,sizeof(BITMAPFILEHEADER),1,f);
fwrite((char*)&bi,sizeof(BITMAPINFOHEADER),1,f);
fwrite(context,dwBmBitsSize,1,f);
fclose(f);
delete context;
::ReleaseDC(NULL,dc);
return 0;
}
void CaptureScreen()
{
int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
HWND hDesktopWnd = GetDesktopWindow();
HDC hDesktopDC = GetDC(hDesktopWnd);
HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,
nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC,hCaptureBitmap);
BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,hDesktopDC,0,0,SRCCOPY);
SaveBmp(hCaptureBitmap, "F:\\capture.bmp"); //Place holder - Put your code
//here to save the captured image to disk
ReleaseDC(hDesktopWnd,hDesktopDC);
DeleteDC(hCaptureDC);
DeleteObject(hCaptureBitmap);
}
void CCaptureScreenDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CaptureScreen();
}
相关文章推荐
- VC获取窗口图片(截图)
- 【截图】WINFORM中几句程序获取整个屏幕的图片及当前窗口的图片快照
- VC MFC 获取屏幕大小 程序窗口大小位置 控件大小位置
- VC++获取窗口句柄(mobile)
- VC 中用 IPicture 在窗口中显示图片文件
- VC 各种情况下的窗口句柄的获取
- 根据视频获取视频截图、并判断截图中图片某个位置为白色
- VC 各种情况下的窗口句柄的获取
- VC获取任务栏窗口及其子窗口句柄
- VC 获取控制台窗口的句柄(hWnd)
- win10 uwp 截图 获取屏幕显示界面保存图片
- VC++界面编程之--获取父窗口背景画刷(附带时钟控件)
- VC编程技术点滴(八)如何在视图类中获取主窗口指针
- VC中获取窗口句柄的各种方法
- VC 中用 IPicture 在窗口中显示图片文件
- iOS 获取屏幕截图和缩放、裁剪图片
- VC 各种情况下的窗口句柄的获取
- VC 窗口用图片做阴影边框
- VC由进程ID获取窗口句柄的各种方
- VC:CListBox(添加项、设置图片、获取总项数、获取选中的项、获取项文本)