how to save rendering to BMP file

Hi all,

DOse anybody know where I can download c/c++ source code that save the rendering scene to a disk BMP file?

anybody can give some hints on this topic?

many thanks

Jerry

Here’s a piece of code that I’ve used in an old project to do that:

bool screenshot(char *fileName){
static unsigned char header[54] = {
0x42, 0x4D, 0x36, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0xC4, 0x0E, 0x00, 0x00, 0xC4, 0x0E, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

unsigned char *pixels = (unsigned char *) malloc(Xres * Yres * 3);
((unsigned __int16 *) header)[ 9] = Xres;
((unsigned __int16 *) header)[11] = Yres;

glReadPixels(0,0,Xres,Yres,GL_RGB,GL_UNSIGNED_BYTE,pixels);

unsigned char temp;
for (unsigned int i = 0; i < Xres * Yres * 3; i += 3){
temp = pixels[i];
pixels[i] = pixels[i + 2];
pixels[i + 2] = temp;
}

HANDLE FileHandle;
unsigned long Size;

if (fileName == NULL){
char file[256];
i = 0;
do {
sprintf(file,“Screenshot%d.bmp”,i);
FileHandle = CreateFile(file,GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
i++;
} while (FileHandle == INVALID_HANDLE_VALUE);
} else {
FileHandle = CreateFile(fileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (FileHandle == INVALID_HANDLE_VALUE) return false;
}

WriteFile(FileHandle,header,sizeof(header),&Size,NULL);
WriteFile(FileHandle,pixels,Xres * Yres * 3,&Size,NULL);

CloseHandle(FileHandle);

free(pixels);
return true;
}

Originally posted by Humus:
[b]Here’s a piece of code that I’ve used in an old project to do that:

[quote]

bool screenshot(char *fileName){
static unsigned char header[54] = {
0x42, 0x4D, 0x36, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0xC4, 0x0E, 0x00, 0x00, 0xC4, 0x0E, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

unsigned char *pixels = (unsigned char *) malloc(Xres * Yres * 3);
((unsigned __int16 *) header)[ 9] = Xres;
((unsigned __int16 *) header)[11] = Yres;

glReadPixels(0,0,Xres,Yres,GL_RGB,GL_UNSIGNED_BYTE,pixels);

unsigned char temp;
for (unsigned int i = 0; i < Xres * Yres * 3; i += 3){
temp = pixels[i];
pixels[i] = pixels[i + 2];
pixels[i + 2] = temp;
}

HANDLE FileHandle;
unsigned long Size;

if (fileName == NULL){
char file[256];
i = 0;
do {
sprintf(file,“Screenshot%d.bmp”,i);
FileHandle = CreateFile(file,GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
i++;
} while (FileHandle == INVALID_HANDLE_VALUE);
} else {
FileHandle = CreateFile(fileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (FileHandle == INVALID_HANDLE_VALUE) return false;
}

WriteFile(FileHandle,header,sizeof(header),&Size,NULL);
WriteFile(FileHandle,pixels,Xres * Yres * 3,&Size,NULL);

CloseHandle(FileHandle);

free(pixels);
return true;
}

[/b][/QUOTE]

thank you very much. I think I will learn much from it.