I have command line c++ project for my final project of my course. I've decided to go a little further than the course intends and write a class for creating an uncompressed bitmap picture file.

As an initial exercise I decided to create the 2x2 pixel bitmap example from Wikipedia. http://en.wikipedia.org/wiki/BMP_file_format#Example_of_a_2x2_Pixel.2C_24-Bit_Bitmap

The problem I'm having is that .bmp file can't be opened by Windows image viewer. I'm not sure where I've gone wrong.

One of the problems with the code is that the size of FileHeader is supposed to be 14 bits, all of the sizes of the internal variables of the structure add up to 14 but when reading the size of the entire structure it is 16 bits.

This is the code for my header:

#ifndef bitmap_saver
#define bitmap_saver

namespace kspace
	class bmp
		typedef struct FileHeader
			unsigned _int16 FileType;
			unsigned _int32 FileSize;
			unsigned _int16 Reserved1;
			unsigned _int16 Reserved2;
			unsigned _int32 BitmapOffset;
		typedef struct BitmapHeader
			unsigned long bmpHeaderSize;
			long Width;
			long Height;
			unsigned short Planes;
			unsigned short BitsPerPixel;
			unsigned long CompressionType;
			unsigned long BMPByteSize;
			long hres;
			long vres;
			unsigned long NumColours;
			long NumImpColours;
		typedef struct ColourPalette
			unsigned _int8 Blue;
			unsigned _int8 Green;
			unsigned _int8 Red;
			unsigned _int8 buffer;

		//Function prototyping
		void bmp_create();


And for the function definition

#include "bitmap_saver.h"
#include <iostream>
#include <fstream>

using namespace std;
using namespace kspace;

void bmp::bmp_create()
	int sz =0;

		FileHeader fileHeader;


		sz += sizeof(fileHeader);
		cout << sizeof(fileHeader.BitmapOffset)+sizeof(fileHeader.FileSize)+sizeof(fileHeader.FileType)+sizeof(fileHeader.Reserved1)+sizeof(fileHeader.Reserved2) << endl;
		cout << sz << endl;
		BitmapHeader bmpHeader;


		sz += sizeof(bmpHeader);
		cout << sizeof(bmpHeader) << endl;

		ColourPalette a[4];

		for(int i=1;i<4;i++)

		sz += sizeof(a);

		cout << sizeof(a) << endl;

		cout << sz << endl;
	fstream test;

	test.open("test.bmp", ios::out|ios::binary);
	test.write(reinterpret_cast<char*>(&fileHeader), sizeof(fileHeader));
	test.write(reinterpret_cast<char*>(&bmpHeader), sizeof(bmpHeader));
	test.write(reinterpret_cast<char*>(&a), sizeof(a)); 
Great first question - code (tagged of course), evidence of research and a question

> for(int i=1;i<4;i++)
This doesn't initialise ALL the elements.

> bmpHeader.BitsPerPixel=24;
You've said the format is 3 bytes per pixel - so why are you writing 4?

Padding for 4 byte alignment (Could be a value other than zero)

You forgot to take into account this little detail.

Generally, you need to assemble a byte array yourself, then write that out.
Pointing at some struct is generally a bad approach, even when it seems to work.

Edited 6 Years Ago by Salem: n/a

Thank you Salem! I've got it working now. I really should have seen the error in the for loop. I fixed the problems with the padding. I didn't create a byte array to store the information (I don't know how- unless it's just an array of characters and I store all the values in there). Instead I used #pragma pack(1) to set the alignment of the structure to the one I want.

Thank you again for your help.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.