[英]Convert PrintWindow to BitBlt to capture screenshot of a specific window
我有一个C ++程序来捕获特定窗口的屏幕截图并使用以下代码保存它
int main()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
RECT rc;
HWND hwnd = FindWindow(NULL,TEXT("Window Title Here"));
if(hwnd == NULL)
{
cout<<"Can't Find Window";
return 0;
}
GetClientRect(hwnd,&rc);
HDC hdcscreen = GetDC(NULL);
HDC hdc = CreateCompatibleDC(hdcscreen);
HBITMAP hbmp = CreateCompatibleBitmap(hdcscreen,rc.right - rc.left,rc.bottom - rc.top);
SelectObject(hdc,hbmp);
PrintWindow(hwnd,hdc,NULL);
BitmapToJpg(hbmp,rc.right - rc.left,rc.bottom-rc.top); //Function to convert hbmp bitmap to jpg
DeleteDC(hdc);
DeleteObject(hbmp);
ReleaseDC(NULL,hdcscreen);
}
此代码适用于许多窗口,但对于某些窗口,输出是具有正确宽度和高度的黑色图像。 在搜索时,我找到了一个使用BitBlt()
的解决方案。 但我无法弄清楚如何用BitBlt()
替换PrintWindow()
BitBlt()
并输出到HBITMAP
。 帮助需要
首先,更换hdcscreen
由hdcwnd
,你用得到GetDC(hwnd)
代替GetDC(NULL)
它可能不会改变任何东西,但它更适合,即使使用PrintWindow()
。
然后,只需替换:
PrintWindow(hwnd,hdc,NULL);
通过:
BitBlt( hdc, 0, 0, rc.right - rc.left,rc.bottom-rc.top, hdcwnd, 0, 0, SRCCOPY );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.