C++ 使用位图计算位图的内存使用情况
我可以使用GetObject(…)从HBITMAP句柄获取位图结构 但我想知道是否有一种方法可以计算该图像的内存使用率(或内存占用?) 我不想显示内存大小超过xyz的图像C++ 使用位图计算位图的内存使用情况,c++,visual-c++,bitmap,C++,Visual C++,Bitmap,我可以使用GetObject(…)从HBITMAP句柄获取位图结构 但我想知道是否有一种方法可以计算该图像的内存使用率(或内存占用?) 我不想显示内存大小超过xyz的图像 typedef struct tagBITMAP { LONG bmType; LONG bmWidth; LONG bmHeight; LONG bmWidthBytes; WORD bmPlanes; WORD bm
typedef struct tagBITMAP
{
LONG bmType;
LONG bmWidth;
LONG bmHeight;
LONG bmWidthBytes;
WORD bmPlanes;
WORD bmBitsPixel;
LPVOID bmBits;
} BITMAP, *PBITMAP, NEAR *NPBITMAP, FAR *LPBITMAP;
lpb位;包含所有数据,但如何计算
void*
指针的大小?如果我记得清楚,示意图是扫描线*高度加上调色板大小,因此应该是:
bmWidthBytes * bmHeight + bmPlanes * bmBitsPixel
位图结构是一种没有调色板信息的老式定义。 它保持兼容。 我认为它现在通常是由BITMAPINFO定义的
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;
这是CreateDIBSection()API的参数
此API获取像素区域。
这个大小通常是“bibibitcount/8*宽度*高度”(在没有托盘的情况下)你有使用的像素数(就像你有宽度和高度),然后你有每个像素的位数。只需要简单的乘法,大概是宽度*高度*比特像素/8。这没有什么意义,当你得到这个信息时,损害已经造成了,你已经加载了位图,并用光了它的内存。所以你最好显示它。@JoachimPileborg,你是说
bmWidth*bmHeight*bmBitsPixel
?但是bmPlanes
和bmWidthBytes
在什么地方合适呢?或者它们与实际大小无关?@HansPassant,“8”来自哪里?@SimonGoodman每字节8位。结构中的一些信息是冗余的。
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;
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);