0
votes

I can get a BITMAP struct from a HBITMAP handle using GetObject( ... )

HBITMAP hSrc = // source image
BITMAP bDst;
GetObject(hSrc, sizeof(BITMAP), &bDst);
...

But was wondering if there was a way of calculating the memory usage, (or footprint?) of that image.

I don't want to display images that are more than xyz in size of memory.

typedef struct tagBITMAP
{
  LONG        bmType;
  LONG        bmWidth;
  LONG        bmHeight;
  LONG        bmWidthBytes;
  WORD        bmPlanes;
  WORD        bmBitsPixel;
  LPVOID      bmBits;
} BITMAP, *PBITMAP, NEAR *NPBITMAP, FAR *LPBITMAP;

LPVOID bmBits; contains all the data, but how can I calculate the size of that void* pointer?

2
You have the number of pixels used (as in you have the width and height) and then you have number of bits per pixel. Some simple multiplication is all you need.Some programmer dude
It is roughly width * height * bitsperpixel / 8. There is very little point to this, by the time you get this info the damage is already done, you have already loaded the bitmap and used up the memory for it. So you might as well display it.Hans Passant
@JoachimPileborg, you saying bmWidth*bmHeight*bmBitsPixel? But then where does bmPlanes and bmWidthBytes fit in or are they not involved in the actual size?Simon Goodman
@HansPassant, where is the '8' from?Simon Goodman
@SimonGoodman 8 bits per byte. And some of the information in the structure is redundant.Some programmer dude

2 Answers

0
votes

If I remember well, the footprint is the scanline * height plus the palette size, so it should be:

bmWidthBytes * bmHeight + bmPlanes * bmBitsPixel
0
votes

BITMAP structure is old type of definition without the information of color palette. It remains for compatible. I think it is defined by BITMAPINFO usually now.

typedef struct tagBITMAPINFO {
    BITMAPINFOHEADER bmiHeader;
    RGBQUAD          bmiColors[1];
} BITMAPINFO;
typedef struct tagBITMAPINFOHEADER{
    DWORD  biSize;
    LONG   biWidth; 
    LONG   biHeight;
    WORD   biPlanes;
    WORD   biBitCount;     // (1, 4, 8, 16, 24, 32) 24 or lager→RGB, others→using color palette
    DWORD  biCompression;
    DWORD  biSizeImage;
    LONG   biXPelsPerMeter;
    LONG   biYPelsPerMeter;
    DWORD  biClrUsed;
    DWORD  biClrImportant;
} BITMAPINFOHEADER;

That is a parameter for CreateDIBSection() API.

BITMAPINFO bmi;
BITMAPFILEHEADER* lpbmpfh = &bmi.bmiHeader;
lpbmpfh->biWidth = width;
lpbmpfh->biHeight = height;
lpbmpfh->biBitCount = 24;
void* lpPixel;
HBITMAP hDib = CreateDIBSection(NULL,&bmi,DIB_RGB_COLORS,(void**)&lpPixel,NULL,0);

This API gets pixel region. This size is "biBitCount / 8 * width * height" usually.(in the case of no pallete)