How much memory should be allocated for the DIB data received from HBITMAP using GetDIBits function?

会有一股神秘感。 提交于 2019-12-11 04:15:44

问题


How much memory should be allocated for the DIB data received from HBITMAP using GetDIBits function?

The GetDIBits function is described in MSDN as follow:

int GetDIBits(
  __in     HDC hdc,
  __in     HBITMAP hbmp,
  __in     UINT uStartScan,
  __in     UINT cScanLines,
  __out    LPVOID lpvBits,
  __inout  LPBITMAPINFO lpbi,
  __in     UINT uUsage
);

However, the buffer to receive data lpvBits must be allocated before calling GetDIBits, because GetDIBits doesn't allocate this automatically.

The question is how much memory should be allocated to receive the DIB data? Supposed that the HBITMAP has width&height as Bmp_Width&Bmp_Height; and the bitmap is 32-bit (RGBA).


回答1:


I think the simplest way is calling GetObject() function (And BTW to get the image bits):

BITMAP bmpObject;
GetObject(hBitmap, sizeof(BITMAP), &bmpObject);

Then you simply use the Bitmap Fields:

LONG size = bmpObject.bmWidthBytes * bmpObject.bmHeight;

Be aware of alignment whem processing image bytes!

Hope this will be helpful!




回答2:


The memory pointed to by lpvBits must be the size of one scan line times the height. Each scan line must be aligned on a DWORD boundary.

Since you are using 32 bit colour then each scanline will naturally satisy that requirement, so long as you ensure that the first scanline, i.e. the start of the memory block, is 4 byte aligned.

So the answer, measured in bytes, is 4*width*height, aligned to start on a 4 byte boundary.



来源:https://stackoverflow.com/questions/7304749/how-much-memory-should-be-allocated-for-the-dib-data-received-from-hbitmap-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!