Solved

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

Posted on 2015-01-13
3
259 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 34

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 34

Accepted Solution

by:
sarabande earned 500 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Header of docx file 17 129
Safe conversion? 4 75
C++ mouse_event mouse look 7 110
Error building VS2105 solution from repository 1 33
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

713 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