Thread: reading bmp file

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    18

    reading bmp file

    my project is to read bmp picture and to take the parmerter of hieght and print it,when im trying to do this file and my output is wrong,i dont know why
    my code:
    Code:
    struct BitMap
    {
        int Bits;
        int Size;
        int Width;
        int Height;
        
    } Header;
    
        FILE *BMPFile;    fopen_s(&BMPFile,"pic.bmp", "r");
        memset(&Header, 0, sizeof(Header));
    
    
        fread(&Header.Size, 4, 1, BMPFile);
        fread(&Header.Width, 4, 1, BMPFile);
        fread(&Header.Height, 4, 1, BMPFile);
    
    
        printf("biSize:%d\n", Header.Size);
        printf("Width:%d\n", Header.Width);
        printf("Height:%d\n", Header.Height);
        fclose(BMPFile);
    my output is :
    biSize:2066632002
    Width:25
    Height:3538944

    what am i doing wrong ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    A couple of things.

    1. When reading binary data on Windows, use the mode "rb".

    2. BMP file format - Wikipedia
    Define a structure which actually matches what a bitmap looks like.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by Salem View Post
    A couple of things.

    1. When reading binary data on Windows, use the mode "rb".

    2. BMP file format - Wikipedia
    Define a structure which actually matches what a bitmap looks like.
    Windows encourages this approach in its own functions for manipulating .bmp files. However really it is bad way of doing it. C can run on big or litle-endian machines, and the sizes of basic types can vary. You also sometimes get unexpected padding in structures.

    It's better to read field by field, with code that will work regardless of the endianness or integer size of the platform. See my website for a source file which helps you do this (the iee754 project).
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Mar 2020
    Posts
    18
    Quote Originally Posted by Malcolm McLean View Post
    Windows encourages this approach in its own functions for manipulating .bmp files. However really it is bad way of doing it. C can run on big or litle-endian machines, and the sizes of basic types can vary. You also sometimes get unexpected padding in structures.

    It's better to read field by field, with code that will work regardless of the endianness or integer size of the platform. See my website for a source file which helps you do this (the iee754 project).

    hey ,thanl you very much.
    so i need to use big indian to read this file ?
    the bmp file is a map/

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by cbeginner12334 View Post
    so i need to use big indian to read this file ?
    From the wiki article:

    "All of the integer values are stored in little-endian format (i.e. least-significant byte first)."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-27-2013, 12:24 PM
  2. Replies: 0
    Last Post: 03-21-2013, 03:31 PM
  3. Replies: 3
    Last Post: 11-28-2012, 09:16 AM
  4. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  5. Replies: 13
    Last Post: 05-31-2009, 11:30 AM

Tags for this Thread