为什么GetObject返回一个带有空bmBits的BITMAP? [英] Why does GetObject return an BITMAP with null bmBits?

查看:723
本文介绍了为什么GetObject返回一个带有空bmBits的BITMAP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:我试图截取另一个窗口,将它送入OpenCV。我在网上找到了一些代码,应该能够将BITMAP转换为OpenCV可以使用的东西。不幸的是我遇到了一些麻烦。



问题:为什么bmBits属性/成员总是null? (我也尝试用PrintWindow而不是BitBlt的结果是一样的)

  #include< iostream> 
#include< string>
#include< Windows.h>

int main(int argc,char * argv [])
{
std :: wstring windowName = LCalculator;

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;
}


解决方案

bmBits 成员对于DIB节为非空。对于设备相关位图(例如您正在创建的位图), bmBits 未设置,因为像素在视频卡上,而不是在主存储器中。 p>

在您的示例中,您需要将 CreateCompatibleBitmap 更改为 CreateDIBSection 如果你想直接访问这些位。


Context: I'm trying to take a screenshot of another window to feed it into OpenCV. I found some code on the web that should be able to convert a BITMAP to something OpenCV can work with. Unfortunately I ran into some trouble.

Question: Why is the bmBits attribute/member always null? (I also tried with PrintWindow instead of BitBlt the result was the same)

#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;
}

解决方案

The bmBits member is non-null for DIB sections. For device-dependent bitmaps (such as the one you're creating), the bmBits is not set because the pixels are on the video card, not in main memory.

In your example, you need to change CreateCompatibleBitmap to CreateDIBSection if you want direct access to the bits.

这篇关于为什么GetObject返回一个带有空bmBits的BITMAP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆