Get bytes from HBITMAP

后端 未结 5 1846
逝去的感伤
逝去的感伤 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条回答
  •  -上瘾入骨i
    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.

提交回复
热议问题