Create a image file (BMP, JPG) from a OpenGL view

Create and save a image file(bmp, jpg) from a OpenGL View

What´s your question???

Most direct way is a glReadPixels from the color buffer with GL_BGRA_EXT into the DIBits of a 32 bit DIBitmap.

How I can obtain images (jpg, bmp) of a MFC view opengl. With PDF_DRAW_TO_BITMAP?

glReadPixels is the normal way, heaps of examples out there. one on my site under the BGR extension

I dump screen captures into an uncompressed .TGA

I just use glReadPixels using GL_RGB to specify the format I wish to have the pixels. I put the pixels into a buffer allocated with (width * height * 3) bytes.

One thing that has to be recalled is that glReadPixels gives you the screen’s info upside down. Fortunately, this is the same way that .TGAs are stored. However, I do have to do a little work with the data, because it is necessary to flip the R and B values of each pixel because .TGA stores pixels as BGR not RGB. You can find .TGA information any where on the net, and building an acceptable header for the file is quite simple.

Good luck

void CaptureScreen()

unsigned char *image	= (unsigned char*)malloc(sizeof(unsigned char)*Width*Height*3);
FILE *file				= fopen("capture.bmp", "wb");

if( image!=NULL )
	if( file!=NULL ) 
		glReadPixels( 0, 0, Width, Height, GL_BGR_EXT, GL_UNSIGNED_BYTE, image );

		memset( &bf, 0, sizeof( bf ) );
		memset( &bi, 0, sizeof( bi ) );

		bf.bfType			= 'MB';
		bf.bfSize			= sizeof(bf)+sizeof(bi)+Width*Height*3;
		bf.bfOffBits		= sizeof(bf)+sizeof(bi);
		bi.biSize			= sizeof(bi);
		bi.biWidth			= Width;
		bi.biHeight			= Height;
		bi.biPlanes			= 1;
		bi.biBitCount		= 24;
		bi.biSizeImage		= Width*Height*3;
		fwrite( &bf, sizeof(bf), 1, file );
		fwrite( &bi, sizeof(bi), 1, file );
		fwrite( image, sizeof(unsigned char), Height*Width*3, file );

		fclose( file );
	free( image );


It´snt necessary use PDF_DRAW_TO_BITMAP. I use this procedure…

ReadBitmap(SIZE sizePixels){
long i, j;
GLubyte *rgb,temp;
void *m_bits;
long m_bitsize;

// Activate The Rendering Context

// Clear The Screen And The Depth Buffer

m_bits = NULL;
// …
glPixelStorei(GL_UNPACK_ALIGNMENT, 4); /* Force 4-byte alignment */
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
glPixelStorei(GL_PACK_SKIP_ROWS, 0);
glPixelStorei(GL_PACK_SKIP_PIXELS, 0);

long width;
if(m_bits == NULL){
width = * 3;
width = (width + 3) & ~3;
m_bitsize = width *;
m_bits = calloc(m_bitsize, 1);

glReadPixels(0,0,,, GL_RGB, GL_UNSIGNED_BYTE,

// Swap red and blue for the bitmap…

for (i = 0; i <; i ++)
for (j = 0, rgb = ((GLubyte *)m_bits) + i * width;j <;j ++, rgb += 3){
temp = rgb[0];
rgb[0] = rgb[2];
rgb[2] = temp;


sizePixels is the size of the view.
m_bits are the bits of the bitmap
m_bitsize the size of m_bits…