简体   繁体   English

Windows 10版本1703(15063.138)上的bitblt失败

[英]bitblt failed on Windows 10 version 1703 (15063.138)

using Visual Studio 2017, vc141, the following code should got a screenshot from front game window but now it return a black and blank image. 使用Visual Studio 2017 vc141,以下代码应从游戏前窗口获取屏幕截图,但现在它返回黑白图像。

only issue with games(tried OpenGL and Vulkan, ogl return black, vulkan return white) 游戏唯一的问题(尝试过OpenGL和Vulkan,ogl返回黑色,vulkan返回白色)

before upgrade to windows 10 1703, it works on windows 10 1607 and windows 7 sp1 在升级到Windows 10 1703之前,它可以在Windows 10 1607和Windows 7 sp1上运行

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

code: 码:

BOOL ScreenShot(cv::Mat *img, HWND hWnd = NULL) {
    HBITMAP hBitmap;
    HDC hdcSys = GetDC(hWnd);
    HDC hdcMem = CreateCompatibleDC(hdcSys);

    void *ptrBitmapPixels;
    BITMAPINFO bi;
    HDC hdc;

    RECT rect;

    if (!GetWindowRect(hWnd, &rect) || (hWnd == NULL)) {
        return FALSE;
    }

    ZeroMemory(&bi, sizeof(BITMAPINFO));

    LONG lWidth = rect.right - rect.left;
    LONG lHeight = rect.bottom - rect.top;

    bi.bmiHeader.biSize = sizeof(BITMAPINFO);
    bi.bmiHeader.biWidth = lWidth;
    bi.bmiHeader.biHeight = -lHeight;
    bi.bmiHeader.biPlanes = 1;
    bi.bmiHeader.biBitCount = 32;

    hdc = GetDC(hWnd);
    hBitmap = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, &ptrBitmapPixels, NULL, 0);

    SelectObject(hdcMem, hBitmap);

    *img = cv::Mat(lHeight, lWidth, CV_8UC4, ptrBitmapPixels, 0);

    BitBlt(hdcMem, 0, 0, lWidth, lHeight, hdcSys, 0, 0, SRCCOPY);

    //DeleteObject(hBitmap);
    DeleteDC(hdcMem);
    ReleaseDC(hWnd, hdcSys);
    ReleaseDC(hWnd, hdc);

    return TRUE;
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    /*...*/
    HotKeyId = GlobalAddAtom(L"DBKGNDSCREENSHOT");
    RegisterHotKey(hWnd, HotKeyId, NULL, VK_F10);
    /*...*/
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    /*...*/
    case WM_HOTKEY:
        if (wParam == HotKeyId) {
            cv::Mat t;
            HWND MainHWND;
            MainHWND = GetForegroundWindow();

            ScreenShot(&t, MainHWND);
            cv::imshow("1", t);
        }
        break;
    /*...*/
}

and still black even PrintWindow(at least we got a titlebar) 甚至是黑色的PrintWindow(至少我们有一个标题栏)

PrintWindow(hWnd, hdcMem, 0);
//BitBlt(hdcMem, 0, 0, lWidth, lHeight, hdcSys, 0, 0, SRCCOPY);

I send this program to my friend (without any modify, his OS=win7 x64), but he got the correct result. 我将此程序发送给了我的朋友(不做任何修改,他的OS = win7 x64),但是他得到了正确的结果。

so what should I do? 所以我该怎么做?

GDI is a very old technology and is slowly getting deprecated. GDI是一项非常古老的技术,并逐渐被弃用。 The more reliable method to capture desktop on Windows 10 would be through Desktop Duplication API . 在Windows 10上捕获桌面的更可靠方法是通过桌面复制API

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

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