-
October 7th, 2015, 03:43 PM
#1
Write BITMAP data to file in 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;
04
05 unsigned char m_cHeaderData[1078];
06 unsigned char** m_cImageData = new unsigned char* [height];
07
08 for( int i = 0; i <height; i++)
09 {
10 m_cImageData[i] = new unsigned char [width];
11 }
12
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
18
19 for(int i = 0; i <height; i++)
20 {
21 m_pInFile->read(reinterpret_cast<char*>(m_cImageData[i]), width);
22 }
23
24 m_pInFile->close();
25
26 ofstream* m_pOutFile;
27 m_pOutFile = new ofstream;
28 m_pOutFile->open("imageCopy.bmp", ios: ut | 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();
35
36
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;
-
October 8th, 2015, 01:34 AM
#2
Re: Write BITMAP data to file in C++
Please, format your code properly with indents and place it within the CODE tags!
Victor Nijegorodov
-
October 8th, 2015, 03:48 AM
#3
Re: Write BITMAP data to file in C++
Bitmap files have their own headers and data in special format . Pl read about that . You will get the file format on NET. The file starts with BM ...
Last edited by new_2012; October 8th, 2015 at 04:01 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|