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