14

I'm a newbie in programming bmp files and i checked this web site to learn about bmp header.. http://www.daubnet.com/en/file-format-bmp

it seems that the header of a bmp file is 54 bytes.

Using paint, i created a simple 10x10 image, and i saved it in 24 bits. so according to simple math, the file size should be 10*10*3 + 54 = 354 bytes.

but hex editor and file explorer returned a size of 374 bytes.

So i have a difference of 20 bytes, and i don't know why.

could you tell me why please?

thanks a lot!!

nneonneo
  • 171,345
  • 36
  • 312
  • 383
user1657743
  • 141
  • 1
  • 2
  • 5
  • Padding for alignment? Use `SizeOf` instead of calculating it yourself. (Also, it helps if you mention the programming language you're asking about; it makes it much easier to provide an answer.) – Ken White Sep 09 '12 at 06:25
  • 2
    Well, he's using MSPaint, not a programming language :) – nneonneo Sep 09 '12 at 06:26
  • @nneonneo: "I'm a newbie in programming bmp files" is the opening sentence of the question. – Ken White Sep 09 '12 at 06:30

1 Answers1

16

Lines in BMPs are padded out to multiples of 4 bytes.

Without padding, each line is 3*10 = 30 bytes. With padding, each line is 32 bytes, so the image data is 320 bytes in size. Thus, the file size is 54+320 = 374 bytes.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • 1
    Hi, Thanks a lot for your answer. That makes sense... but in my program, i have a matrix of 10x10 pixels, each pixel is RGB. So i have an extra of 20 bytes of raster data to calculate ? I'm a little bit confused ... thanks again for your help! – user1657743 Sep 09 '12 at 06:53
  • @user1657743: you don't have to put anything specific in the padding; it's just garbage bytes that nobody (except steganographers) will see. – nneonneo Aug 27 '13 at 05:34