0

Trying to get the valid size of BMP file.

Of course the best way is just to get the Length property of the loaded stream.

But BMP header format DOES include the information about its size and I want to try to get it exactly from BMP header.

As from Wiki or other source:

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

offset: 0002h | 4 bytes | the size of the BMP file in bytes 

So the size value is included in BMP header in region of 4 bytes ( from [2] -> [5]: 2, 3, 4, 5 )

So first of all I thought to get all byte values and sum the all:

1).

int BMPGetFileSize(ref byte[] headerPart)
{
    int fileSize = 0;

    for (int i = 0; i < headerPart.Length; i++)
    {
        fileSize += headerPart[i];
    }

    return (fileSize > 0) ? fileSize : -1;
}

I've got a very small size... For my file, the actual size is 901:

enter image description here

But, I've got from the code: 84.

I've checked for the correct region, I thought that I can get not the correct values, BUT I've got it correctly ( from 2nd till 5th from byte[] data of BMP ).

2). Then I thought, that I must not sum them, but just to write at one string line all the values and then convert it to System.Int32 and divide on 1024 to get the size in Kbytes, but again... it doesn't equal to the 901 Kb value.

enter image description here

You may thought, that I've confused the region and selecting wrong values, when you look at watch dialog and compare it with the function code, but as you see the byte[] array from function is: headerPart, not data, so I didn't confuse anything, data[] is the file stream of whole BMP file.

So, how could I get file size from BMP header, not from the property of stream in C#?

Secret
  • 2,627
  • 7
  • 32
  • 46

1 Answers1

1

The BMP file format is a binary format, which means you cannot read it using a StreamReader or TextReader (which are both used only for text) or decode it with a UTF-8 or ANSI decoder. (Encodings are also used only for text.) You have to read it using a BinaryReader.

The documentation states:

offset: 0002h | 4 bytes | the size of the BMP file in bytes

So you need to read four bytes and combine them into an integer value.

With the BinaryReader class you can call the ReadUInt32() method to read 4 bytes that form an unsigned 32-bit integer.

If you do that, you'll see it reads:

921654

...which is 900 KiB and then some.

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
  • don't understand why little-endian is here, couldn't BMP file be an UTF-8 or ANSI? I've get the encoding of file: `using(var fileStreamReader = new StreamReader(fileName, true)) { fileEncoding = fileStreamReader.CurrentEncoding; }` and it shows exact for my file, which I've described exactly here in UTF-8 encoding not Little-Endian. And I don't understand, why do you use bytes shift for the `[3], [4], [5]` at all, could you consult me please, thank you! – Secret Mar 23 '13 at 16:28
  • @OlegOrlov If you open a .bmp file in a text editor, does it look like text or does it look like random garbage? It will look like garbage, which means it is a _binary_ file, _not a text file_. You cannot read binary files with a `StreamReader` or `TextReader`, and it doesn't use encoding. You have to use a `BinaryReader` instead, and forget about the bit shifting if that confuses you. – Daniel A.A. Pelsmaeker Mar 23 '13 at 16:30
  • I see, but why do you use exactly shifting in from 0 till 24, not for example from 24 till 48 in byte shifting? I'm confused with such shifting operands, because the values, which you shifted are not from the beginning of the file, so why did you begin to shift from `<< 8`, where there values are not the start values? thanks! – Secret Mar 23 '13 at 16:37
  • @OlegOrlov Ignore the shifting, its way to complex for what you're trying to achieve. You don't need to know it. If you still really want to know it, copy what I did [from my old revision](http://stackoverflow.com/revisions/da76ac3c-cdde-46b5-a59a-fb41a9b16efb/view-source) and ask a new question on Stackoverflow. It has nothing to do with where you are in the file. – Daniel A.A. Pelsmaeker Mar 23 '13 at 16:43