Write Bitmap Image Data to a New Image File C++

Hello all. I am currently trying to copy the header and pixel data from an image file and write this into a new image file. I am using a 256 colour bitmap and for testing have chosen a fixed height and width pixel of 150 and 90. A new file image file is created but when trying to load this, the following message is given: "Paint cannot read this file. This is not a valid bitmap file or its format is currently not supported. Would appreciate the support after many attempts. Below is the code I'm using:

02         const int height = 150;  

03         const int width = 90;  


05     unsigned char m_cHeaderData[1078];  

06     unsigned char** m_cImageData = new unsigned char* [height];  


08     for( int i = 0; i <height; i++)  

09     {  

10         m_cImageData[i] = new unsigned char [width];  

11     }  


13     ifstream* m_pInFile;      

14     m_pInFile = new ifstream;  

15     m_pInFile->open("image.bmp", ios::in | ios::binary);  

16     m_pInFile->seekg(0, ios::beg);  

17     m_pInFile->read(reinterpret_cast<char*>(m_cHeaderData), 1078); //bitmap bits start at offset 1078  


19     for(int i = 0; i <height; i++)  

20     {  

21         m_pInFile->read(reinterpret_cast<char*>(m_cImageData[i]), width);   

22     }  


24     m_pInFile->close();  


26     ofstream* m_pOutFile;   

27     m_pOutFile = new ofstream;  

28     m_pOutFile->open("imageCopy.bmp", ios::out | ios::trunc | ios::binary);      

29     m_pOutFile->write(reinterpret_cast<char*>(m_cHeaderData), 1078);  

30     for(int i = 0; i <height; i++)      

31     {  

32         m_pOutFile->write(reinterpret_cast<char*>(m_cImageData[i]), width;  

33     }  

34     m_pOutFile->close();  



37     //deallocate memory:  

38     delete m_pInFile;  

39     delete m_pOutFile;  

40     for(int i = 0; i <height; i++)  

41     {  

42         delete[] m_cImageData[i];  

43     }  

44     delete[] m_cImageData;  


Open in new window

Thanks for the feedback in advance!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

check whether the size of inputfile and outputfile is identically. if not, some of your assumptions are wrong.

for example that the BITMAPFILEHEADER + size of palette has a size of 1078.

see https://msdn.microsoft.com/en-us/library/windows/desktop/dd183391(v=vs.85).aspx

for details.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.