drawing pixels in a bitmap

Hi everyone! Im a little new to c++ and this forum, so please correct me if this is not in the right topic area. My question is how can I create a Bitmap image from code? I have been researching this for a while now, but I cant understand how to make the whole thing work. The only thing I managed to understand about a Bitmap image is that it works as a multidimensional array, or a 2D array, and each pixel in the image is a position in the array. I already know how to create the image file, but now I want to be able to add pixel data (or color data to the array positions) to this file. Any help is appreciated, thank you!

P.S: I put my code for creating the bitmap image below, please tell me if this is not the proper way of doing this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <fstream>
using namespace std;



int main ()
{
	ofstream myImage;
	myImage.open("My New Bitmap.bmp");
	
	//cool stuff goes here...

	myImage.close();
}
The short answer is that you dig out the specification of the bitmap format:

http://en.wikipedia.org/wiki/BMP_file_format

and you write each byte according to that.

The longer answer is to look at some code that already does this:

http://ubuntuforums.org/archive/index.php/t-920409.html

Finally, pick out a library that does this sort of thing and use one of those :)
I recommend SDL or OpenGL for drawing pixels, good luck! :)
Topic archived. No new replies allowed.