Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9110351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:14:55+00:00 2026-06-17T03:14:55+00:00

I would like to read a bitmap file into a struct and manipulate it

  • 0

I would like to read a bitmap file into a struct and manipulate it like eg. making a mirror effect, but I cannot understand which kind of struct should I be creating in order to read into it.

Thank you for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:14:56+00:00Added an answer on June 17, 2026 at 3:14 am

    »This is how you manually load a .BMP file

    The bitmap file format:

    • Bitmap file header
    • Bitmap info header
    • Palette data
    • Bitmap data

    So on with the code part. This is our struct we need to create to hold the bitmap file header.

    #pragma pack(push, 1)
    
    typedef struct tagBITMAPFILEHEADER
    {
        WORD bfType;  //specifies the file type
        DWORD bfSize;  //specifies the size in bytes of the bitmap file
        WORD bfReserved1;  //reserved; must be 0
        WORD bfReserved2;  //reserved; must be 0
        DWORD bfOffBits;  //specifies the offset in bytes from the bitmapfileheader to the bitmap bits
    }BITMAPFILEHEADER;
    
    #pragma pack(pop)
    

    The bftype field checks to see if you are in fact loading a .BMP file, and if you are, the field should be 0x4D42.

    Now we need to create our bitmapinfoheader struct. This holds info about our bitmap.

    #pragma pack(push, 1)
    
    typedef struct tagBITMAPINFOHEADER
    {
        DWORD biSize;  //specifies the number of bytes required by the struct
        LONG biWidth;  //specifies width in pixels
        LONG biHeight;  //specifies height in pixels
        WORD biPlanes;  //specifies the number of color planes, must be 1
        WORD biBitCount;  //specifies the number of bits per pixel
        DWORD biCompression;  //specifies the type of compression
        DWORD biSizeImage;  //size of image in bytes
        LONG biXPelsPerMeter;  //number of pixels per meter in x axis
        LONG biYPelsPerMeter;  //number of pixels per meter in y axis
        DWORD biClrUsed;  //number of colors used by the bitmap
        DWORD biClrImportant;  //number of colors that are important
    }BITMAPINFOHEADER;
    
    #pragma pack(pop)
    

    Now on to loading our bitmap.

    unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
    {
        FILE *filePtr;  //our file pointer
        BITMAPFILEHEADER bitmapFileHeader;  //our bitmap file header
        unsigned char *bitmapImage;  //store image data
        int imageIdx=0;  //image index counter
        unsigned char tempRGB;  //our swap variable
    
        //open file in read binary mode
        filePtr = fopen(filename,"rb");
        if (filePtr == NULL)
            return NULL;
    
        //read the bitmap file header
        fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER),1,filePtr);
    
        //verify that this is a .BMP file by checking bitmap id
        if (bitmapFileHeader.bfType !=0x4D42)
        {
            fclose(filePtr);
            return NULL;
        }
    
        //read the bitmap info header
        fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER),1,filePtr); 
    
        //move file pointer to the beginning of bitmap data
        fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
    
        //allocate enough memory for the bitmap image data
        bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
    
        //verify memory allocation
        if (!bitmapImage)
        {
            free(bitmapImage);
            fclose(filePtr);
            return NULL;
        }
    
        //read in the bitmap image data
        fread(bitmapImage,bitmapInfoHeader->biSizeImage,1,filePtr);
    
        //make sure bitmap image data was read
        if (bitmapImage == NULL)
        {
            fclose(filePtr);
            return NULL;
        }
    
        //swap the R and B values to get RGB (bitmap is BGR)
        for (imageIdx = 0;imageIdx < bitmapInfoHeader->biSizeImage;imageIdx+=3)
        {
            tempRGB = bitmapImage[imageIdx];
            bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
            bitmapImage[imageIdx + 2] = tempRGB;
        }
    
        //close file and return bitmap image data
        fclose(filePtr);
        return bitmapImage;
    }
    

    Now to make use of all of this:

    BITMAPINFOHEADER bitmapInfoHeader;
    unsigned char *bitmapData;
    // ...
    bitmapData = LoadBitmapFile("mypic.bmp",&bitmapInfoHeader);
    //now do what you want with it, later on I will show you how to display it in a normal window
    

    Later on I’ll put up Writing to a .BMP, and how to load a targa file, and how to display them.«

    Quoted from: http://www.vbforums.com/showthread.php?261522-C-C-Loading-Bitmap-Files-%28Manually%29 (User: BeholderOf). (Some minor corrections done)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to read a text file and input its contents into an
I would like to read a text file into an array of strings using
I would like to read a website asynchronously, which isnt possible with urllib as
I would like to read a DICOM file in C#. I don't want to
I would like to read the last 1 megabyte of a MP3 file and
I would like to read a file in R as a table for a
I would like to read only the first 8 characters of a text file
I would like to read a thin YAML file using a simple C program.
I would like to read mp3 tags from mp3 file :D and save it
I would like to read a Lua file in a level editor so I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.