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 HDC, which I can't get.
EDIT: I load bitmap from file:
HBITMAP bm = 0;
BITMAP Bitmap;
bm = (HBITMAP)LoadImage (0, TEXT("C:\\img1.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
I pass the HBITMAP to the function and expect to get another HBITMAP of processed image:
HBITMAP out1 = func(bm);
Where func is:
HBITMAP func(HBITMAP im);
And the problem is how to get image bytes from HBITMAP.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With