Hi, here is a little Yule gift for all you GL junkies out there, it’s a short quick and easy tool for taking a screen shot into the tga format, sure it’s uncompressed and doesn’t increment the file name, but it’s pretty slick.

void screenshot (char filename[160],int x, int y)
{// get the image data
long imageSize = x * y * 3;
unsigned char *data = new unsigned char[imageSize];
glReadPixels(0,0,x,y, GL_BGR,GL_UNSIGNED_BYTE,data);// split x and y sizes into bytes
int xa= x % 256;
int xb= (x-xa)/256;int ya= y % 256;
int yb= (y-ya)/256;//assemble the header
unsigned char header[18]={0,0,2,0,0,0,0,0,0,0,0,0,(char)xa,(char)xb,(char)ya,(char)yb,24,0};

// write header and data to file
fstream File(filename, ios::out | ios::binary);
File.write (reinterpret_cast<char *>(header), sizeof (char)*18);
File.write (reinterpret_cast<char *>(data), sizeof (char)*imageSize);

delete[] data;

Usage: screenshot (“test.tga”,800,600);
Just call the function with the correct window size just before SwapBuffers and it will take a screen shot of everything in the frame buffer, easy.


  • By yifan, December 10, 2011 @ 06:52

    very nice function. I use it to save my ray tracing picture.

  • By Peter Wallström, December 10, 2011 @ 12:47

    Tanks, and pretty soon i will release an even better version in my toolkit that can deal with compression

  • By NaliPL, February 7, 2012 @ 01:15

    change to reinterpret_cast

    require include :

    if not using “using namespace std;

    Then place std before; std::fstream std::ios

  • By Alex, February 17, 2012 @ 12:32

    Hi, I get a Expected ‘<' before '(' token in both reinterpret_cast lines.
    Any way to fix it? Thanks

  • By Alex, February 17, 2012 @ 13:39

    Well, I actually solved it using:


  • By memmaker650, September 30, 2012 @ 23:49

    I have problems using your method.

    First of all, it throws 2 errors in the ‘reinterpret_cast’ lines.

    It compiles changing they to:
    File.write(reinterpret_cast (&header), sizeof(char)*18);
    File.write(reinterpret_cast (&data), sizeof(char)*imageSize);

    Now my program compiles and works fine except when I try to save an screenshot. It saves me a tga file that cannot be open but with a size of 18Kb.

    You know how to solve that. What’s the right option to use the reinterpret_cast instruction ???

  • By Peter Wallström, October 1, 2012 @ 00:46

    Well clearly in c++ header and &header is not the same thing, so that’s a problem right there, while i haven’t tested it i am pretty sure it’s pointing those arrays where they shouldn’t.
    try this code instead, i use it in my latest toolkit.

    File.write (reinterpret_cast(header), sizeof (char)*18);
    File.write (reinterpret_cast
    (data), sizeof (char)*imageSize);

  • By Kai, June 2, 2016 @ 09:19

    Thank you for the clean code. However, I think it’s better if there is alpha channel support.

  • By Peter Wallström, June 2, 2016 @ 18:10

    thats easy
    change long imageSize = x * y * 3; to long imageSize = x * y * 4;
    change GL_BGR to GL_BGRA
    and finally replace the header with
    unsigned char header[18]={0,0,2,0,0,0,0,0,0,0,0,0,(char)xa,(char)xb,(char)ya,(char)yb,32,0};

Other Links to this Post

RSS feed for comments on this post. TrackBack URI

Leave a comment


WordPress Themes