# wanting to know how many bytes per pixel in a bitmap?

### Question

• Does anyone know what the code is to figure out how many bytes are in an image?  I am trying to figure it out but have had no luck in doing so yet.   Thank you in advance.
Tuesday, August 28, 2012 3:28 AM

• There are many types of bitmaps.
There are also different pixel formats for them. Some use palettes and some don't.
Make sure that you load it as a binary file. I made that mistake
In windows bitmaps, any palette entries use 4 bytes.
There is a short(16-bit integer) starting at the 29th byte in the file that determines the pixel format(bits per pixel).
Look at a few *.bmp files in a text editor to see how information is stored in them.
The width is a 4 byte integer starting at the 19th byte of the file.
The height is a 4 byte integer starting at the 23rd byte.
Here are some explanations of a few pixel formats.
1: monochrome, 8 pixels stored in 1 byte, uses 2 palette entries.
4: 16 colour, 2 pixels stored in 1 byte, uses 16 palette entries.
8: 256 colour, 1 pixel in 1 byte, uses a 256 colour palette
16: 2 bytes per pixel, uses r,g,b values(5 bits each)
24: 3 bytes per pixel, uses r,g,b values(8 bits each)
32: 4 bytes per pixel, uses an alpha byte(basically unused) and r,g,b values(8 bits each)
If the bits per pixel is less than or equal to 8(using a palette), the palette entries start at the 55th byte of the bitmap.
The pixel data starts right after the palette entries. If the palette isn't used, the pixel data starts at the 55th byte.

• Proposed as answer by Tuesday, August 28, 2012 6:31 AM
• Marked as answer by Tuesday, August 28, 2012 4:36 PM
Tuesday, August 28, 2012 6:31 AM

### All replies

• Check out bob powell's tutorial on lockbits, it should help you understand. It is by far the most thorough tutorial on lockbits I have found.(yes this is relevant to your question of how many bytes per pixel) read the tutorial to see how...

http://www.bobpowell.net/lockingbits.htm

If you want something you've never had, you need to do something you've never done.

Tuesday, August 28, 2012 3:52 AM
• Thanks Paul I read Bob Powell's tutorial on lockbits but it did not cover total bits for an image.
Tuesday, August 28, 2012 4:02 AM
• Thanks Paul I read Bob Powell's tutorial on lockbits but it did not cover total bits for an image.

BMPPixelCount = BMPHeight * BMPWidth

BMPByteCount = (BMPBitsPerPixel *BMPPixelCount) /8

You need to make sure you know the bits per pixel format of your bitmap, the best way to do this is to make sure that you reformat your bmp to a specific format...

If you want something you've never had, you need to do something you've never done.

Tuesday, August 28, 2012 4:09 AM
• See this function for converting your bitmap to a known format:

http://www.freevbcode.com/ShowCode.asp?ID=5799

If you want something you've never had, you need to do something you've never done.

Tuesday, August 28, 2012 4:12 AM
• Thanks Paul I read Bob Powell's tutorial on lockbits but it did not cover total bits for an image.

Like for example: This image format "Format32BppArgb"

Is 32 bits per pixel, Alpha, red, green, blue,

See how the format tells you?

So all you need to do is multiply the "bitsperpixel" times the total pixels in the image. you will then have the total bits in the image, you then divide that by 8, and you will have the total byte count for the bitmap's data chunk

If you want something you've never had, you need to do something you've never done.

Tuesday, August 28, 2012 4:16 AM
• This takes the image from PictureBox and writes it to file on disk then counts the bytes in the file and diplays the amount in RichTextBox and deletes the file. Probably the long way round about doing it though.

```Imports System.IO

Public Class Form1

Dim bmp As Bitmap
bmp = PictureBox1.Image
bmp.Save("C:\Users\John\Desktop\J1.Bmp", bmp.RawFormat)

Dim info As New FileInfo("C:\Users\John\Desktop\J1.Bmp")
Dim length As Long = info.Length
RichTextBox1.AppendText(CStr(length) & " bytes")

File.Delete("C:\Users\John\Desktop\J1.Bmp")

End Sub

Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click

End Sub

Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged

End Sub
End Class```

You've taught me everything I know but not everything you know.

Tuesday, August 28, 2012 4:37 AM
• I want to warn you just in case you are looking to only get the byte count of a certain part of the bitmap file(such as the count of bytes that the actual pixel data uses), then measuring the entire size of the file may not give you the result you are looking for. I also found this interesting illustration of the binary structure of a bitmap. Enjoy.

http://en.wikipedia.org/wiki/BMP_file_format

If you want something you've never had, you need to do something you've never done.

Tuesday, August 28, 2012 4:55 AM
• There are many types of bitmaps.
There are also different pixel formats for them. Some use palettes and some don't.
Make sure that you load it as a binary file. I made that mistake
In windows bitmaps, any palette entries use 4 bytes.
There is a short(16-bit integer) starting at the 29th byte in the file that determines the pixel format(bits per pixel).
Look at a few *.bmp files in a text editor to see how information is stored in them.
The width is a 4 byte integer starting at the 19th byte of the file.
The height is a 4 byte integer starting at the 23rd byte.
Here are some explanations of a few pixel formats.
1: monochrome, 8 pixels stored in 1 byte, uses 2 palette entries.
4: 16 colour, 2 pixels stored in 1 byte, uses 16 palette entries.
8: 256 colour, 1 pixel in 1 byte, uses a 256 colour palette
16: 2 bytes per pixel, uses r,g,b values(5 bits each)
24: 3 bytes per pixel, uses r,g,b values(8 bits each)
32: 4 bytes per pixel, uses an alpha byte(basically unused) and r,g,b values(8 bits each)
If the bits per pixel is less than or equal to 8(using a palette), the palette entries start at the 55th byte of the bitmap.
The pixel data starts right after the palette entries. If the palette isn't used, the pixel data starts at the 55th byte.

• Proposed as answer by Tuesday, August 28, 2012 6:31 AM
• Marked as answer by Tuesday, August 28, 2012 4:36 PM
Tuesday, August 28, 2012 6:31 AM