Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-01-13
3
Medium Priority
?
277 Views
Last Modified: 2015-01-19
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
0
Comment
Question by:jxbma
  • 2
3 Comments
 
LVL 35

Expert Comment

by:sarabande
ID: 40548671
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
0
 
LVL 1

Author Comment

by:jxbma
ID: 40550183
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
0
 
LVL 35

Accepted Solution

by:
sarabande earned 2000 total points
ID: 40550966
if you include windows.h you also may call winapi from console program.

if you have a client that returns a HBITMAP, the same client also could return a valid HDC (handle to device context).

but before you change the interface,  you could try another thing. if you were able to retrieve an LPBITMAP (what is a pointer to BITMAP struct), you may check the member pmbits whether it is not NULL. pmbits is a pointer to the color bits what should be the same byte array  as you would get with GetDIBits.

the size of the byte array could be calculated by pbm->bmWidth*pbm->bmHeight*sizeof(RGBQUAD) where pbm is the LPBITMAP pointer.

Sara
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question