实现代码如下:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>
using namespace std;

#define BMP_Header_Length 54
#define WindowWidth 400
#define WindowHeight 400

static bool gbSaveBMP =false;

typedef struct tagPoint {
    GLfloat fx;
    GLfloat fy;
}Point;

Point point[] = {
    {   0.0f,  0.0f},
    {  70.0f,  0.0f},
    {   0.0f, 70.0f},
    {  70.0f, 70.0f},
};


static const Point kDestPoint = {0.0f, 0.0f};
static GLfloat kFactor = 1.0f;
static GLfloat kRotateAngle = 0.0f;

static bool gbRotate = false;


void displayShape();
void reShape(GLint w, GLint h);
void handleMessage(unsigned char key, GLint x, GLint y);
void idleFunc();

void readPixels() {

    FILE*     pDummyFile = 0;
    FILE*     pWritingFile = 0;
    GLubyte*  pPixelData = 0;
    GLubyte   BMP_Header[BMP_Header_Length];
    GLint     pixelWidth = WindowWidth * 3;//3 is the 24 bit of the BMP file format 
    GLint     j = 0;
    GLint     PixelDataLength = 0;

    while( pixelWidth % 4 != 0 )       // 补充数据,直到i是的倍数
        ++pixelWidth;               

    PixelDataLength = pixelWidth * WindowHeight;// get the num of the pixels is width * heigh
    // 分配内存和打开文件
    pPixelData = (GLubyte* )malloc(PixelDataLength);
    memset(pPixelData, 0, PixelDataLength);
    if( pPixelData == 0 )
        exit(0);
    pDummyFile = fopen("d:\\dummy.bmp", "rb");
    if( pDummyFile == 0 )
        exit(0);

    pWritingFile = fopen("d:\\grab.bmp", "wb");
    if( pWritingFile == 0 )
        exit(0);
    // 读取像素
    glFlush();
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
    glReadPixels(0, 0, 
               WindowWidth, WindowHeight,
			   GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
    //把dummy.bmp的文件头复制为新文件的文件头GL_BGR_EXT
    fread(BMP_Header, sizeof(BMP_Header), 1, pDummyFile);
    fwrite(BMP_Header, sizeof(BMP_Header), 1, pWritingFile);
    fseek(pWritingFile, 0x0012, SEEK_SET);

    pixelWidth = WindowWidth;
    j = WindowHeight;
    fwrite(&pixelWidth, sizeof(pixelWidth), 1, pWritingFile);
    fwrite(&j, sizeof(j), 1, pWritingFile);

    // 写入像素数据
    fseek(pWritingFile, 0, SEEK_END);
    fwrite(pPixelData, PixelDataLength, 1, pWritingFile);

    // 释放内存和关闭文件
    fclose(pDummyFile);
    fclose(pWritingFile);
    free(pPixelData);
}

/*
* 
*/

void handleMessage(GLint key, GLint x, GLint y) {
    switch(key)
    {
    case GLUT_KEY_LEFT:
        
        gbRotate = true;
        break;
    case GLUT_KEY_RIGHT:
        kFactor -= 0.01f;
        gbRotate = true;
        break;
    case GLUT_KEY_UP:
        kRotateAngle -= 0.01f;
        gbRotate = false;
        break;
    case GLUT_KEY_DOWN:
        kRotateAngle += 0.01f;
        gbRotate = false;
        break;
    default:
        break;
    }

    glLoadIdentity();//flush Screen;
}

void reShape(GLint w, GLint h) {
    if(h == 0) h = 1;  
    glViewport(0, 0, w, h);  

    glMatrixMode(GL_PROJECTION);  
    glLoadIdentity();  

    if(w <= h) {  
        glOrtho(-200.0f, 200.0f * h / w, -200.0f, 200.0f * h / w, -200.0f, 200.0f);  
    } else {  
        glOrtho(-200.0f * w / h, 200.0f * w / h, -200.0f, 200.0f, -200.0f, 200.0f);  
    }

    glMatrixMode(GL_MODELVIEW);  
    glLoadIdentity();  


}

void idleFunc() {
    displayShape();
}

void displayShape() {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0f, 1.0f, 0.0f);
    glBegin(GL_QUAD_STRIP);
    GLint length = sizeof(point) / sizeof(point[0]);
    for(GLint i = 0; i < length; ++i) {
        glVertex2f(point[i].fx, point[i].fy);
    }  
    glEnd();
	glutSwapBuffers();

	readPixels();//after drawing the pixels to the screen, the get the pixels from the buffer
}

int main(int argc, char *argv[]) {
    glutInit(&argc, argv); 
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(400, 400);
    glutCreateWindow("Five-Pointed Star");
    glutDisplayFunc(displayShape);
    glutSpecialUpFunc(handleMessage);
    glutReshapeFunc(reShape);
    glutIdleFunc(idleFunc);
    glutMainLoop();
    return 0;
}



需要注意一点的是:在创建bmp文件的时候一定是真彩图(24位位图),不然会失败导致的结果要么是黑色要么什么都没有。