Tetramputechture
Tetramputechture

Reputation: 2921

How to get exact bitmap file size?

I'm trying to get the correct width and height of a bitmap file via its hex info. I've got the width and height specified in the header, but I need to adjust the width due to padding and then calculate the size of the file in bytes.

The formula I'm using right now is

(width * height * colorDepth)/8 + 54

First of all, is this formula correct, and second, how would I adjust the width for padding?

Upvotes: 2

Views: 3423

Answers (1)

ForguesR
ForguesR

Reputation: 3618

Calculating total size of a bmp file is not something very simple. The formula you provided leads me to believe that you are looking for a very specific bmp file type with :

This effectively gives a header size of 54 bytes.

In a bmp file, each rows of pixels is rounded to a multiple of 4 bytes (the row is padded). Therefore to obtain the real size of a bmp you need to calculate the row size with padding, multiply it by the image height and add the header size.

To calculate the row size with padding (in bytes) use this formula : ceiling(width * bpp / 32) * 32 / 8.

To calculate the total size (in bytes) of the bmp you can use : (height * ceiling(width * bpp / 32) * 32) / 8 + 54.

Where bpp is bits-per-pixel (color depth).

Upvotes: 2

Related Questions