Solved

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

Posted on 2015-01-13
3
258 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 33

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 33

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 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