opengl-Saving BMP Pictures

Source: Internet
Author: User

BOOL screenshot (const char* filename)
Glenum Lastbuffer;
glbyte* pbits = 0; Image data
unsigned long limagesize;
Glint Iviewport[4]; View Size

Glgetintegerv (Gl_viewport, Iviewport);
Limagesize = iviewport[2] * iviewport[3] * 3;

Pbits = (glbyte*) new unsigned char[limagesize];
if (!pbits)
return false;

Reading data from the color buffer
Glpixelstorei (gl_pack_alignment, 1);
Glpixelstorei (gl_pack_row_length, 0);
Glpixelstorei (gl_pack_skip_rows, 0);
Glpixelstorei (gl_pack_skip_pixels, 0);

Glgetintegerv (Gl_read_buffer, (glint*) &lastbuffer);
Glreadbuffer (Gl_front);
Glreadpixels (0, 0, iviewport[2], iviewport[3], Gl_bgr_ext, Gl_unsigned_byte, pbits);
Glreadbuffer (Lastbuffer);

if (writebmp (filename, (unsigned char*) pbits, iviewport[2], iviewport[3]))
return true;

return false;


BOOL Writebmp (const char filename[], unsigned char* data, unsigned int w, unsigned int h)
Std::ofstream Out_file;

/** Check Data */
if (!data)
Std::cerr << "Data corrupted!" << Std::endl;
Out_file.close ();
return false;

/** Create bitmap file information and bitmap file header structure */
Bitmapfileheader header;
Bitmapinfoheader Bitmapinfoheader;

unsigned char texturecolors = 0;/**< is used to transform the image color from BGR to RGB */

/** Open the file and check for errors */ (filename, std::ios::out | std::ios::binary);
if (!out_file)
Std::cerr << "Unable to open file" << filename << Std::endl;
return false;

/** Filling Bitmapfileheader */
Header.bftype = bitmap_id;
Header.bfsize = w*h*3 + sizeof (bitmapfileheader) + sizeof (bitmapinfoheader);
header.bfreserved1 = 0;
Header.bfreserved2 = 0;
header.bfoffbits = sizeof (Bitmapfileheader) + sizeof (bitmapinfoheader);
/** Writing bitmap file header information */
Out_file.write (char*) &header, sizeof (Bitmapfileheader));

/** Filling Bitmapinfoheader */
bitmapinfoheader.bisize = sizeof (Bitmapinfoheader);
Bitmapinfoheader.biwidth = W;
Bitmapinfoheader.biheight = h;
Bitmapinfoheader.biplanes = 1;
Bitmapinfoheader.bibitcount = 24;
Bitmapinfoheader.bicompression = Bi_rgb; Bi_rle4 Bi_rle8
Bitmapinfoheader.bisizeimage = w * H * 3; When the compression type is BI_RGB, it can also be set to 0
Bitmapinfoheader.bixpelspermeter = 0;
Bitmapinfoheader.biypelspermeter = 0;
bitmapinfoheader.biclrused = 0;
bitmapinfoheader.biclrimportant = 0;
/** writing bitmap File information */
Out_file.write (char*) &bitmapinfoheader, sizeof (Bitmapinfoheader));

/** move the pointer to the beginning of the data */
OUT_FILE.SEEKP (Header.bfoffbits, Std::ios::beg);

/** Writing image Data */
Out_file.write ((char*) data, bitmapinfoheader.bisizeimage);

Out_file.close ();
return true;

opengl-Saving BMP Pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.