PDA

View Full Version : Screen capture



miikkal
04-07-2001, 09:47 AM
How could I capture screen from my opengl program. The captured screen should be in .bmp mode. Is there some easy/short way doing it?

dans
04-07-2001, 03:22 PM
I think you need to include
#include <mmsystem.h>

usage call

screenshot(int startx, int starty, int width, int height, const char *fname)

example:
screenshot(0,0,1024,768,"screenshot.bmp");

i got the source code which had to be modified from elsewhere on this board.

int write_screenshot_bmp(const char *fname, const unsigned char *image, int w, int h)
{
BITMAPFILEHEADER 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; //__attribute__((packed)) info;

FILE *fp;
int ret = 0;
fp = fopen(fname, "wb");
if (!fp) {
// unable to open file for writing
ret = -1;
}
else{
header.bfType = 'MB';
header.bfSize = sizeof(header) + sizeof(info) + w * h * 3;;
header.bfReserved1 = 0;
header.bfReserved2 = 0;
header.bfOffBits = sizeof(header) + sizeof(info);
info.size = sizeof(info);
info.width = w;
info.height = h;
info.planes = 1;
info.bit_count = 24;
info.comp = 0;
info.sizeImage = w * h * 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, fp);
fwrite(&info, sizeof(info), 1, fp);

fwrite(image, sizeof(unsigned char), h*w*3, fp);
}
if (fp){
fclose(fp);
}
return ret;
}

int screenshot(int x, int y, int width, int height, const char *fname)
{
unsigned char *image = NULL;
int ret = 0;

// reserve some mem
image = (unsigned char*)malloc(sizeof(unsigned char)*width*height*3);
if (!image){
// unable to allocate image buffer
ret = -1;
}

else{
glReadPixels(x,y,width,height, GL_BGR_EXT, GL_UNSIGNED_BYTE, image);
ret = write_screenshot_bmp(fname, image, width, height);
}

if (image) {
free(image);
}

return ret;
}

rts
04-07-2001, 06:38 PM
Wow... that source looks familiar... *ahem* http://www.opengl.org/discussion_boards/ubb/smile.gif

If it is mine, I'd like to point out that that source is GPLed... please respect my copyright and choice of license. Thanks.


[This message has been edited by rts (edited 04-07-2001).]

pleopard
04-07-2001, 07:24 PM
Originally posted by dans:
i got the source code which had to be modified from elsewhere on this board

miikkal
04-08-2001, 11:58 PM
Thanks, but if my program runs in 32bit mode should I do any changes to it? That code ins't as short as I expected. You can load bmp-file easily with LoadImage function, but there isn't any function which would save hdc to bmp-file or something like that???

miikkal
04-09-2001, 06:10 AM
I made some changes to code...

void CaptureScreen( int Width, int Height )
{
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;

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 );
}
}

dovkruger
02-11-2004, 10:24 AM
Source code to my library, currently running in Linux but should run anywhere, is available at:
http://richardson.dl.stevens-tech.edu/dov/SDV