C++窗口截图输出的大小与该窗口大小不同

3
我有一个在C++中捕获BlueStacks模拟器窗口像素的简短程序,然后我将使用OpenCV操纵像素,根据一些决策发送鼠标输入(win32api)。我在Visual Studio Enterprise 2017中工作。唯一的问题是,该函数似乎只捕获了比完整窗口更小的像素区域。这里是一个示例图像。原始窗口在左侧,镜像输出在右侧。

example

我应该如何解决这个问题?我已经在我的项目设置中启用了高 DPI 感知,除了在函数中添加随机魔法数字之外,不确定还需要采取哪些步骤。 我已经用 Python 编写过这个程序,但我想为了性能提升重新用 C++ 重写。 这里是一个视频(警告:噪音)。还有一个。 这是我的当前代码:
#include "stdafx.h"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <Windows.h>
#include <iostream>

using namespace std;
using namespace cv;

Mat hwnd2mat(HWND hwnd);

int main(int argc, char **argv)
{
    printf("Hello, world!\n");
    Sleep(1000);

    HWND hwndDesktop;

    hwndDesktop = GetForegroundWindow();
    // namedWindow("output", WINDOW_NORMAL);
    int key = 0;
    Mat src;

    while (key != 27)
    {
        src = hwnd2mat(hwndDesktop);
        // you can do some image processing here
        imshow("output", src);
        key = waitKey(1); // you can change wait time
    }

}

Mat hwnd2mat(HWND hwnd)
{
    HDC hwindowDC, hwindowCompatibleDC;

    int height, width, srcheight, srcwidth;
    HBITMAP hbwindow;
    Mat src;
    BITMAPINFOHEADER  bi;

    hwindowDC = GetDC(hwnd);
    hwindowCompatibleDC = CreateCompatibleDC(hwindowDC);
    SetStretchBltMode(hwindowCompatibleDC, COLORONCOLOR);

    RECT windowsize;    // get the height and width of the screen
    GetClientRect(hwnd, &windowsize);

    srcheight = windowsize.bottom;
    srcwidth = windowsize.right;
    height = windowsize.bottom / 1;  //change this to whatever size you want to resize to
    width = windowsize.right / 1;

    src.create(height, width, CV_8UC4);

    // create a bitmap
    hbwindow = CreateCompatibleBitmap(hwindowDC, width, height);
    bi.biSize = sizeof(BITMAPINFOHEADER);    //http://msdn.microsoft.com/en-us/library/windows/window/dd183402%28v=vs.85%29.aspx
    bi.biWidth = width;
    bi.biHeight = -height;  //this is the line that makes it draw upside down or not
    bi.biPlanes = 1;
    bi.biBitCount = 32;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = 0;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrUsed = 0;
    bi.biClrImportant = 0;

    // use the previously created device context with the bitmap
    SelectObject(hwindowCompatibleDC, hbwindow);
    // copy from the window device context to the bitmap device context
    StretchBlt(hwindowCompatibleDC, 0, 0, width, height, hwindowDC, 0, 0, srcwidth, srcheight, SRCCOPY); //change SRCCOPY to NOTSRCCOPY for wacky colors !
    GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, src.data, (BITMAPINFO *)&bi, DIB_RGB_COLORS);  //copy from hwindowCompatibleDC to hbwindow

                                                                                                       // avoid memory leak
    DeleteObject(hbwindow);
    DeleteDC(hwindowCompatibleDC);
    ReleaseDC(hwnd, hwindowDC);

    return src;
}
2个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
3
拉伸现象是由于DPI缩放引起的。你自己的程序不具备DPI感知功能,而另一个程序似乎有DPI感知功能。使你的程序具备DPI感知功能最简单的方法是在程序开始时调用SetProcessDPIAware();。 另外,当调用GetDIBits时,HBITMAP句柄不应该被选择在设备上下文中。你可以改写你的代码如下:
SetProcessDPIAware();
...

Mat hwnd2mat(HWND hwnd)
{
    RECT rc;
    GetClientRect(hwnd, &rc);
    int width = rc.right;
    int height = rc.bottom;

    Mat src;
    src.create(height, width, CV_8UC4);

    HDC hdc = GetDC(hwnd);
    HDC memdc = CreateCompatibleDC(hdc);
    HBITMAP hbitmap = CreateCompatibleBitmap(hdc, width, height);
    HBITMAP oldbmp = (HBITMAP)SelectObject(memdc, hbitmap);

    BitBlt(memdc, 0, 0, width, height, hdc, 0, 0, SRCCOPY);
    SelectObject(memdc, oldbmp);

    BITMAPINFOHEADER  bi = { sizeof(BITMAPINFOHEADER), width, -height, 1, 32, BI_RGB };
    GetDIBits(hdc, hbitmap, 0, height, src.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS);

    DeleteObject(hbitmap);
    DeleteDC(memdc);
    ReleaseDC(hwnd, hdc);

    return src;
}

这段代码仅适用于本机Win32程序。对于其他程序,如Chrome、WPF、Qt应用程序,将显示空白屏幕。您需要截取桌面窗口的屏幕截图。


谢谢你的建议 :) 不过,奇怪的是,下面的项目设置不够吗?https://i.imgur.com/NoPyDgA.png(在Visual Studio Enterprise 2017中)我现在就试试你的建议。 - Hexus_One
好的,我已经尝试添加了该方法以及新函数 - 程序输出与原帖中的图像完全相同 :/ 我在函数中添加了一个常量值来表示高度和宽度,这对我的需求已经足够了,虽然我不确定是否需要更改其他值。例如:int width = rc.right + 100 int height = rc.bottom + 100; - Hexus_One
你的 DPI 方法是首选,它设置了清单。不要添加 SetProcessDPIAware。在问题中还有其他未显示的内容。 - Barmak Shemirani
奇怪...我重新启动了我的笔记本电脑,然后它就正常工作了(我的现有函数会扫描窗口大100个像素,去除常数后会正确地扫描窗口)。我可以告诉你,我的 DPI 确实设置为 200%,而且我正在使用一台 3840x2160 的屏幕。我现在还发现 BlueStacks 窗口本身是可调整大小的。我将在这里链接我的当前代码:https://pastebin.com/12L56jXx - Hexus_One
有时候 DPI 的更改不会立即生效,除非您重新启动或注销并登录。 - Barmak Shemirani
修正 DeleteDC(memdc),注意不要删除oldbmp,因为它并非由此程序分配。 - Barmak Shemirani

0

谢谢 :) 我正在使用GetForegroundWindow()作为占位符来查找程序的窗口标题 - 我会尽快用FindWindow()替换它。不过还是谢谢你提供的链接,我会仔细阅读,因为它似乎有一个重要的区别。 - Hexus_One
我尝试了你的建议(直接将GetForegroundWindow()替换为GetActiveWindow()),但无论如何它似乎都会评估为NULL :/ - Hexus_One
原来我从const char*到LPCSTR的转换出了问题 - 我应该使用TEXT()宏进行转换。但我仍然在想,为什么GetActiveWindow()在我的情况下会返回NULL呢?毕竟我至少有一个活动窗口。 - Hexus_One

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,