Get bytes from HBITMAP

后端 未结 5 1844
逝去的感伤
逝去的感伤 2021-01-13 13:01

How can I get image bytes from hbitmap if I am given an HBITMAP pointer, and my application is console application. I tryed using GetDIBits which require such parameter as

相关标签:
5条回答
  • 2021-01-13 13:13

    You can try GetBitmapBits API even from console. More here: http://msdn.microsoft.com/en-us/library/windows/desktop/dd144850%28v=vs.85%29.aspx

    Just pass HBITMAP handle and you'll get your bytes

    0 讨论(0)
  • 2021-01-13 13:16

    Since you're using LoadImage to get the HBITMAP, then it is indeed a DIB (Device-Independent Bitmap) (they call it a DIBsection). However, you don't have the color information.

    This MSDN HOWTO shows you how to select the DIBsection into a memory DC. They then go on to use GetDIBColorTable to get the palette. However, I believe from there, with that DC you can use GetDIBits to get the RGB bitmap information as you were trying to do.

    Here's the general gist of it:

    // Create a memory DC and select the DIBSection into it
    hMemDC = CreateCompatibleDC( NULL );
    (HBITMAP)SelectObject( hMemDC, hBitmap );
    
    GetDIBits(hMemDC, hBitmap, ...);
    

    You'll note in their code that SelectObject returns a handle to the what was in the DC. They then restore that before calling DeleteDC. I'm not sure its entirely necessary, but they do it. I left it out here for clarity.

    0 讨论(0)
  • 2021-01-13 13:18

    The easiest way is not to use GetDIBits (nor GetBitmapBits). These functions suck because they copy the data.
    If you want the data directly, just use (for a DDB bitmap)

    BITMAP bitmap;
    GetObject(hBitmap, sizeof(bitmap), (LPVoid)&bitmap);
    

    For a DIB bitmap use

    DIBSECTION dib;
    GetObject(hBitmap, sizeof(dib), (LPVOID)&dib);
    

    GetObject info, See:
    http://msdn.microsoft.com/en-us/library/windows/desktop/dd144904%28v=vs.85%29.aspx

    This will not involve any copying of data, thus avoids the complicated issues associated with GetDIBits, See:
    http://msdn.microsoft.com/en-us/library/windows/desktop/dd144879%28v=vs.85%29.aspx
    Esp. the comments at the bottom for an explanation of the difficulties with GetDIBits.

    Note that you do not get the palette info, but since most bitmaps are 24 or 32 bits anyway this is hardly an issue most of the time.

    0 讨论(0)
  • 2021-01-13 13:19

    What worked for us is this: call GetDIBits while the correct palette (if indexed colour) is still selected into the device context. (Without the palette selected, the colours got garbled.)

    But in our use case, it turned out that DIB sections performed a lot better, so check those out as well and benchmark. However, there are some gotchas. Windows wouldn't use the palette, we had to call SetDIBColorTable before use. But the device context still needed a one entry (black only) dummy palette selected and realised into the device context otherwise Windows would also ignore the palette set by SetDIBColorTable. Both SetDIBColorTable and RealizePalette needed to be present otherwise the colours would be garbled.

    0 讨论(0)
  • 2021-01-13 13:32

    See new answer since question was edited...

    You cannot do this without a handle to the device context (HDC). This is because GetDIBits expects an HBITMAP which is

    A handle to the bitmap. This must be a compatible bitmap (DDB).

    A DDB is a Device-Dependent Bitmap, (as opposed to a DIB, or Device-Independent Bitmap). That means:

    Note that a DDB does not contain color values; instead, the colors are in a device-dependent format.

    Which is why GetDIBits requires an HDC. Otherwise it cannot get the color information.

    Perhaps a good question is, where did you get an HBITMAP without an accompanying HDC?


    If you're trying to create this bitmap in memory, first you can call CreateCompatibleDC to create an in-memory DC compatible with some device, then CreateCompatibleBitmap with that DC. Then you have an HBITMAP and HDC to work with as you please. Otherwise, if you don't know what your HBITMAP is pointing to, you can't expect to do anything useful with it.

    0 讨论(0)
提交回复
热议问题