背景: 我正在尝试截取另一个窗口的屏幕截图并将其传入OpenCV。我在网上找到了一些代码,应该能够将BITMAP转换为OpenCV可以使用的东西。不幸的是,我遇到了一些问题。
问题: 为什么bmBits属性/成员总是为空?(我也尝试过使用PrintWindow而不是BitBlt,结果相同)
#include <iostream>
#include <string>
#include <Windows.h>
int main(int argc, char* argv[])
{
std::wstring windowName = L"Calculator";
RECT rect;
HWND hwnd = FindWindow(NULL, windowName.c_str());
if (hwnd == NULL)
{
return 0;
}
GetClientRect(hwnd, &rect);
HDC hdcScreen = GetDC(NULL);
HDC hdc = CreateCompatibleDC(hdcScreen);
HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen,
rect.right - rect.left, rect.bottom - rect.top);
SelectObject(hdc, hbmp);
PrintWindow(hwnd, hdc, PW_CLIENTONLY);
BITMAP bmp;
GetObject(hbmp, sizeof(BITMAP), &bmp);
return 0;
}