Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2015-01-13
3
Medium Priority
?
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

721 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