如何计算位图大小? [英] How to calculate bitmap size?

查看:608
本文介绍了如何计算位图大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始开发专门针对Windows的屏幕捕获软件.在MSDN上查找捕获图像我发现自己有些困惑.

当我指的是不包含与实际文件相关的标题等的位图大小时,请记住.我说的是原始像素数据.我以为公式应该是(width*height)*bits-per-pixel.但是,根据示例,这是计算尺寸的正确方法:

DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;

和或:((width*bits-per-pixel + 31) / 32) * 4 * height

我不明白为什么要进行涉及31324的额外计算.也许填充?我不确定,但是任何解释都将不胜感激.我已经尝试过Google搜索,但没有发现任何特别有用的结果.

解决方案

代表位图像素的位打包在行中.通过填充将每行的大小四舍五入为4个字节(32位DWORD)的倍数.

(bits_per_row + 31)/32 * 4确保向上舍入到32位的下一个倍数.答案是以字节为单位,而不是以位为单位,因此是* 4而不是* 32.

请参阅: https://en.wikipedia.org/wiki/BMP_file_format

Started working on screen capturing software specifically targeted for Windows. While looking through an example on MSDN for Capturing an Image I found myself a bit confused.

Keep in mind when I refer to the size of the bitmap that does not include headers and so forth associated with an actual file. I'm talking about raw pixel data. I would have thought that the formula should be (width*height)*bits-per-pixel. However, according to the example this is the proper way to calculate the size:

DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;

and or: ((width*bits-per-pixel + 31) / 32) * 4 * height

I don't understand why there's the extra calculations involving 31, 32 and 4. Perhaps padding? I'm not sure but any explanations would be quite appreciated. I've already tried Googling and didn't find any particularly helpful results.

解决方案

The bits representing the bitmap pixels are packed in rows. The size of each row is rounded up to a multiple of 4 bytes (a 32-bit DWORD) by padding.

(bits_per_row + 31)/32 * 4 ensures the round up to the next multiple of 32 bits. The answer is in bytes, rather than bits hence *4 rather than *32.

See: https://en.wikipedia.org/wiki/BMP_file_format

这篇关于如何计算位图大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆