简体   繁体   English

将PrintWindow转换为BitBlt以捕获特定窗口的屏幕截图

[英]Convert PrintWindow to BitBlt to capture screenshot of a specific window

I have a C++ program to capture the screenshot of a specific window and save it using the following code 我有一个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);
 }

This code works for many of the windows, but for some of the windows, the output is a black image with correct width and height. 此代码适用于许多窗口,但对于某些窗口,输出是具有正确宽度和高度的黑色图像。 On searching I found a solution to use BitBlt() . 在搜索时,我找到了一个使用BitBlt()的解决方案。 But I cannot figure out how to replace PrintWindow() with BitBlt() and output to a HBITMAP . 但我无法弄清楚如何用BitBlt()替换PrintWindow() BitBlt()并输出到HBITMAP Help Need 帮助需要

First, replace hdcscreen by hdcwnd , which you get with GetDC(hwnd) instead of GetDC(NULL) . 首先,更换hdcscreenhdcwnd ,你用得到GetDC(hwnd)代替GetDC(NULL) It probably won't change anything, but it is more adequate, even with PrintWindow() . 它可能不会改变任何东西,但它更适合,即使使用PrintWindow()
Then, just replace : 然后,只需替换:

PrintWindow(hwnd,hdc,NULL);

By : 通过:

BitBlt( hdc, 0, 0, rc.right - rc.left,rc.bottom-rc.top, hdcwnd, 0, 0, SRCCOPY );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM