From Screen to BMP

How to dump the picture on screen to BMP file?

//=======================================================//
// screenshot
// (originally by rts)
//=======================================================//

void screenshot()
{
char fileName[256];
FILE *fBMP;

sprintf(fileName, “shot.bmp”);
if (!(fBMP = fopen(fileName, “wb”))) return;

unsigned char image = (unsigned char) malloc(sizeof(unsigned char) * width * height * 3);
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, image);

#pragma pack(push, 1)

struct BMPHeader
{
unsigned short type;
unsigned int size;
unsigned short res1;
unsigned short res2;
unsigned int offset;
} header;

struct BMPInfo
{
unsigned int size;
unsigned int width;
unsigned int height;
unsigned short planes;
unsigned short bit_count;
unsigned int comp;
unsigned int sizeImage;
unsigned int x_pels_per_meter;
unsigned int y_pels_per_meter;
unsigned int clr_used;
unsigned int clr_important;
} info;

header.type = ‘B’ | ‘M’ << 8;
header.size = sizeof(header) + sizeof(info) + width * height * 3;
header.res1 = header.res2 = 0;
header.offset = sizeof(header) + sizeof(info);
info.size = sizeof(info);
info.width = width;
info.height = height;
info.planes = 1;
info.bit_count = 24;
info.comp = 0;
info.sizeImage = width * height * 3;
info.x_pels_per_meter = info.y_pels_per_meter = 0;
info.clr_used = 0;
info.clr_important = 0;
fwrite(&header, sizeof(header), 1, fBMP);
fwrite(&info, sizeof(info), 1, fBMP);

// swap the image bit order (RGB to BGR)
float tempf;
for (int i = 0; i < info.sizeImage; i += 3)
{
tempf = image[i];
image[i] = image[i + 2];
image[i + 2] = tempf;
}

fwrite(image, sizeof(unsigned char), width * height * 3, fBMP);

free(image);

fclose(fBMP);

#pragma pack(pop)
}

Thanks!

You can also use openIL, which offers an authomatic screenshot function :

– 8< ---- from openIl docs –

ILboolean ilutOglScreenie(ILvoid);

ilutOglScreenie does not modify the current bound image at all.
This function is very specialized and saves the image to a Targa
file with the filename format of screen%d.tga, where %d is a number
from 0 to 126. This function will probably not be suited to most
developers’ preferences.

---- >8 ------------------------------

If you are running windows, a much easier way:

Shift + PrintScreen

Open paint (windows paint program).
Edit ->Paste.
File->Save.

Heh. Well i’m trying to record an video with it.

Oh. In that you case you need to have very fast fingers.