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 7528637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:26:11+00:00 2026-05-30T04:26:11+00:00

Thank you in advance! I’m reading a BMP in C++ using self-defined BITMAPFILEHEADER and

  • 0

Thank you in advance!

I’m reading a BMP in C++ using self-defined BITMAPFILEHEADER and BITMAPINFOHEADER. Then I print it out using SetPixel(). But there’re some problems with the printing order of pixels and I failed to fix it. To illustrate them, please see the images provided. I don’t have enough reputations to upload images here so please check the url. Sorry for the inconvenience:(

http://liuyuel.blogspot.com/2012/02/reading-bmp-in-c.html

Above two are 256*256 lena.bmp. The first one is the original one, and the second one is the result of my program. See the left-most out-of-order bar? It should appear in the right-most as in the original image.

I also tried some random bmp files. But with this one I got a strange color.

(Still see the above link) Its size is 146*146. The third image is the original and fourth is the printed result. I used a screen capturing tool to snip it from the internet, so I don’t know if the problem is in my program (but it didn’t exist in lena.bmp!) or is in the bmp itself.

My code is as below:

/*****BMP.h*****/
#include <fstream>
#include <Windows.h>
#define N 384
using namespace std;

typedef struct {
   WORD bfType;                 /* Magic identifier            */
   DWORD bfSize;                       /* File size in bytes          */
   WORD bfReserved1;
   WORD bfReserved2;
   DWORD bfOffBits;                     /* Offset to image data, bytes */
} HEADER;

typedef struct {
   DWORD biSize;               /* Header size in bytes      */
   LONG biWidth;                /* Width and height of image */
   LONG biHeight;
   WORD biPlanes;       /* Number of colour planes   */
   WORD biBitCount;         /* Bits per pixel            */
   DWORD biCompression;        /* Compression type          */
   DWORD biSizeImage;          /* Image size in bytes       */
   LONG biXPelsPerMeter;     /* Pixels per meter          */
   LONG biYPelsPerMeter;
   DWORD biClrUsed;           /* Number of colours         */
   DWORD biClrImportant;   /* Important colours         */
} INFOHEADER;



/*****main.cpp*****/
#include "BMP.h"

HEADER bmfh;        //bitmap file header
INFOHEADER bmih;    //bitmap info header
BYTE R[N][N], G[N][N], B[N][N];                     //RGB value of every pixel
COLORREF color[N][N];   //color
extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow ();  //initialize the screen
void ReadBmp(char *BmpFileName);
void GetColor(int i, int j);

void main()
{
    HWND hwnd;  //handlers HWND and HDC
    HDC hdc;
    char BmpFileName[10];
    int i, j;

    hwnd = GetConsoleWindow();  //initialize the screen
    hdc = GetDC(hwnd);  
    scanf("%s", BmpFileName);
    ReadBmp(BmpFileName);   //read the BMP file
    for(i = 0; i < bmih.biHeight; i++)
        for(j = 0; j < bmih.biWidth; j++)
            SetPixel(hdc, i, j, color[bmih.biWidth - j][i]);    //draw the BMP image
    ReleaseDC(hwnd,hdc);
}

void ReadBmp(char *BmpFileName)     //read the BMP file
{
    int patch;  //number of 0s for complement in every row
    ifstream in(BmpFileName, ios_base::binary);     //open the BMP file
    in.read((char *)(&bmfh), sizeof(bmfh) - 2);     //read in BITMAPFILEHEADER. Here sizeof returns a value larger than struct size, so "-2"
    if(bmfh.bfType != 0x4d42)                       //if not BMP, exit
    {
        printf("File type is not BMP!\n");
        exit(1);
    }
    in.read((char *)(&bmih),sizeof(bmih));      //read in BITMAPINFOHEADER
    in.seekg(bmfh.bfOffBits, ios_base::beg);    //seek bitmap data
    patch = (4 - (bmih.biWidth * 3) % 4) % 4;   //calculate number of 0s for complement in every row
    for(int i = 0; i < abs(bmih.biHeight); i++) 
    {
        for(int j = 0; j < abs(bmih.biWidth); j++)
        {
            in.read((char *)(&R[i][j]), 1);     //read in data pixel by pixel
            in.read((char *)(&G[i][j]), 1);
            in.read((char *)(&B[i][j]), 1);
            GetColor(i, j);     //obtain the original and greyscaled color
        }
        in.seekg(patch, ios_base::cur); //skip 0s for complement in every row
    }
}

void GetColor(int i, int j)     //obtain the original and greyscaled color
{
    int iB, iG, iR;
    iB = (int)(B[i][j]);
    iG = (int)(G[i][j]);
    iR = (int)(R[i][j]);
    color[i][j] = RGB(iB, iG, iR);  //original color
}
  • 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-05-30T04:26:12+00:00Added an answer on May 30, 2026 at 4:26 am

    The problem is with the structure packing of HEADER (see http://en.wikipedia.org/wiki/Data_structure_alignment for what that means).

    The - 2 in in.read((char *)(&bmfh), sizeof(bmfh) - 2); tries to fix that, but actually won’t. All fields after bfType – including bfOffBits – will have the wrong offset, so their value will not be correct.

    With MSVC you can disable structure packing for HEADERand INFOHEADER like this:

    #pragma pack(push)
    #pragma pack(1)
    
    typedef struct {
    ...
    } HEADER;
    
    typedef struct {
    ...
    } INFOHEADER;
    
    #pragma pack(pop)
    

    Then remove the - 2 and you should be fine.

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

Sidebar

Related Questions

Greetings and thank you in advance for the help. I created a user control
what is webparts in asp.net? a simple example on webparts. thank you in advance.
Thanks in Advance for reading and answer this question. I got button in asp
I am using c# .net. Thanks in advance for any help. I have searched
I'm exporting a schema using ldifde but the output file wraps long lines so
I'd like to bind MenuItemDescription How? Text={Binding Path=LocalizedRessources.MenuItemDescription, Source={StaticResource LocalizedStrings}} In advance thank you
Is possible to pass type as param in SQL function? thank in advance.
while creating custom item renderers which interfaces we need to implement? Thank in advance.
thanks in advance for your help. I am wondering if there is a (design)
Thanks in advance for your help experts. I want to be able to copy

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.