GetBitmapBits - the iSize paramater.

Posted on 2004-11-30
Last Modified: 2013-11-20
This might sound silly, perhaps it is.

If I load an image:


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?

Question by:PMH4514
    LVL 55

    Expert Comment

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

    Expert Comment

    by:Jaime Olivares
    LVL 48

    Accepted Solution

    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.

    Author Comment

    >> 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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
    Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
    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.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now