Link to home
Start Free TrialLog in
Avatar of jxbma
jxbmaFlag for United States of America

asked on

How do I get a byte array[] from a bitmap in Windows (unmanaged) C++?

Hi:

I'm working with BITMAPs/HBITMAP/LPBITMAP in a Windows C++ application.

I'm currently loading a bitmap file into an HBITMAP via:
LPBITMAP lpBitmap = new BITMAP();
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, "BitmapFileName", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBECTION);
GetObject(hBitmap, sizeof(BITMAP), lpBitmap);

Open in new window


I need to get access to the underlying byte array[] for processing.
How do I get the byte array[] from the BITMAP? (without going to a temp file).

I know I can get access to the buffer by directly reading it from the file (via Bitmap header).
But, clients will be passing a BITMAP.

How do I achieve this?

Thanks,
JohnB
Avatar of sarabande
sarabande
Flag of Luxembourg image

to get the byte array of the device independent bitmap you could call the GetDIBits function which takes a HBITMAP as input.

see the following sample code http://msdn.microsoft.com/en-us/library/windows/desktop/dd183402(v=vs.85).aspx

Sara
Avatar of jxbma

ASKER

Hey sarabande:

Thanks for the response.
This would work if I was actually dealing with a windows based app with:
        hdc = GetDC(NULL);
        hdcMem = CreateCompatibleDC(hdc); 

Open in new window


I actually have a console based C++ app, so I don't have hDC's at my disposal.

I know that when I have bitmap file, I can go directly against the file and retrieve the byte array.

In this interface, the client app will be passing me a HBMP, from which I can get to a LPBITMAP.

How do I get the byte array from here?

Thanks,
JohnB
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial