【问题标题】:OpenCV image zoomed in放大的 OpenCV 图像
【发布时间】:2020-12-02 21:30:27
【问题描述】:

我正在使用此功能制作窗口的屏幕截图。它工作正常,但在 Minecraft 等游戏中它会放大。

正常 截屏

cv::Mat getMat(HWND hWND) {
    HDC deviceContext = GetDC(hWND);
    HDC memoryDeviceContext = CreateCompatibleDC(deviceContext);

    RECT windowRect;
    GetClientRect(hWND, &windowRect);

    int height = windowRect.bottom;
    int width = windowRect.right;

    HBITMAP bitmap = CreateCompatibleBitmap(deviceContext, width, height);

    SelectObject(memoryDeviceContext, bitmap);

    //copy data into bitmap
    BitBlt(memoryDeviceContext, 0, 0, width, height, deviceContext, 0, 0, SRCCOPY);

    //specify format by using bitmapinfoheader!
    BITMAPINFOHEADER bi;
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = width;
    bi.biHeight = -height;
    bi.biPlanes = 1;
    bi.biBitCount = 32;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = 0; //because no compression
    bi.biXPelsPerMeter = 1; 
    bi.biYPelsPerMeter = 2; 
    bi.biClrUsed = 3; 
    bi.biClrImportant = 4; 

    cv::Mat mat = cv::Mat(height, width, CV_8UC4); // 8 bit unsigned ints 4 Channels -> RGBA

    //transform data and store into mat.data
    GetDIBits(memoryDeviceContext, bitmap, 0, height, mat.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS);

    //clean up!
    DeleteObject(bitmap);
    DeleteDC(memoryDeviceContext); //delete not release!
    ReleaseDC(hWND, deviceContext);

    return mat;
}

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    根据文档,windowRect.bottom 不是高度。或者windowRect.right 不是宽度。 https://docs.microsoft.com/en-us/windows/win32/api/windef/ns-windef-rect

    所以,你应该这样做:

    int height = windowRect.bottom - windowRect.top;
    int width = windowRect.right - windowRect.left;
    

    编辑1: 我发现了一个和你类似的问题。也许它也可以帮助你。 ClientRect mysteriously smaller than WindowRect?

    干杯。

    【讨论】:

    • 窗口大小与原始窗口相同,但它缩小了我不认为窗口大小有问题,更喜欢我的世界和 valorant,但对于几乎所有其他应用程序它都可以正常工作。
    猜你喜欢
    • 2013-10-20
    • 2021-12-06
    • 2017-04-14
    • 2021-12-23
    • 2012-10-13
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多