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
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
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.
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.
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.
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.