[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

GetBitmapBits - the iSize paramater.

Posted on 2004-11-30
4
Medium Priority
?
363 Views
Last Modified: 2013-11-20
This might sound silly, perhaps it is.

If I load an image:

m_hbmImage = (HBITMAP) ::LoadImage( NULL, strImageName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);

I want to use GetBitmapBits to pull the pixel data into a BYTE array:

BYTE* m_pImage;
m_pImage = new BYTE[iSize];

But the problem is, I need to first determine iSize, which means I first need to know the number of bytes in the image.

I realize that GetBitmapBits returns the actual number retrieved,  MSDN says:

The dwCount parameter specifies the number of bytes to be copied to the buffer. Use CGdiObject::GetObject to determine the correct dwCount value for the given bitmap.

And for GetObject,  MSDN states:

If the object is a CBitmap object, GetObject returns only the width, height, and color format information of the bitmap. The actual bits can be retrieved by using CBitmap::GetBitmapBits.


So the documentation for GetBitmapBits tells me to look at GetObject to determine the size paramater, and the documentation for GetObject tells me to look at GetBitmapBits for the paramater. Circular?

What I did which works, but I don't know if it's "appropriate" and thus I assume there should be a better way, is do:

BYTE* m_pImage = NULL;

int iCount = ::GetBitmapBits(m_hbmImage, 6000000, m_pImage);
m_pImage = new BYTE[iGetCount];
memset(m_pImage, 0, iGetCount);
iCount = ::GetBitmapBits(m_hbmImage, iGetCount, m_pImage);

So I used 6000000 as an arbitrarilly high number, a number higher than any possible image I might be loading. Called GetBitmapBits with that high value only so that I can determine the actual size. Then I called it again, after having initialized the byte array to the proper size.

It seems too complex and just wrong. What is the appropriate way to load an external image, initialize an appropriate sized array and copy the bits of the image into it?

Thanks!
-Paul
0
Comment
Question by:PMH4514
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12709486
A practical approach to reserve buffer space is to assign iSize the lenght of original file.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12709615
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 200 total points
ID: 12713843
Maximum pixel size is 4 bytes. Allocate 4*width*height pixels and this will be always enough for any bitmap. To know bitmap width and height use GetObject API with m_hbmImage.
0
 

Author Comment

by:PMH4514
ID: 12719047
>> Maximum pixel size is 4 bytes. Allocate 4*width*height pixels and this will be always enough for any bitmap.

That sounds reasonable to me.

Thanks
-Paul
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

834 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