[英]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.