Get bytes from HBITMAP

后端 未结 5 1843
逝去的感伤
逝去的感伤 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: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.

提交回复
热议问题