12

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;
}
Borgleader
  • 15,826
  • 5
  • 46
  • 62
  • Are you trying to make a screenshot with this code, just off-hand ? Looks like it. PrintWindow isn't what you need if that is the case. All it does is send a WM_PRINT to the target window with a DC on which to dump. There is no guarantee that window will even process that message, and if so, do what you want. You almost have everything you need, but I believe you need to be using a BitBlt() to pull bits from the screen DC to your compatible DC with your fresh bitmap ready to receive them. – WhozCraig Oct 11 '12 at 04:47

4 Answers4

20

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.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
11

Just for information. When loading bitmap from file and want to use BITMAP .bmBits (for glTexImage2D, glDrawPixels):

LoadImage(NULL, "path_to.bmp", IMAGE_BITMAP, 0, 0,
                                       LR_LOADFROMFILE);

u must specify flag LR_CREATEDIBSECTION

HBITMAP hBmp = NULL;
BITMAP BMp;
hBmp = (HBITMAP) LoadImage(NULL, "bitmap.bmp", IMAGE_BITMAP, 0, 0,
                                       LR_LOADFROMFILE | LR_CREATEDIBSECTION);
GetObject(hBmp, sizeof(BMp), &BMp);
//BMp.bmBits now points to data
befzz
  • 1,232
  • 13
  • 11
5

From GetObject documentation on MSDN. Please note the second paragraph.

If hgdiobj is a handle to a bitmap created by calling CreateDIBSection, and the specified buffer is large enough, the GetObject function returns a DIBSECTION structure. In addition, the bmBits member of the BITMAP structure contained within the DIBSECTION will contain a pointer to the bitmap's bit values.

If hgdiobj is a handle to a bitmap created by any other means, GetObject returns only the width, height, and color format information of the bitmap. You can obtain the bitmap's bit values by calling the GetDIBits or GetBitmapBits function.

Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
-2

One thing which you could do is to look at the return value of GetObject. If 0 you know something has gone wrong. Something wrong with the parameters of the call.

Owen Ransen
  • 369
  • 2
  • 11