Screenshot with OpenGL


I’m new to openGL and I want to migrate my screenshot program, written in C and using Windows API, to openGL.
It does BitBlt/GetDIBits/etc. and saves the desktop image as bmp-file.

However, I have problems with OpenGL with this simple task.

Can Anyone show me an example of such a task?

Below is my C-code, however the saved file is always black…

HWND hwnd;
int width = 1024;
int height = 768;

BYTE pRGB = (BYTE )malloc(3widthheight);

hwnd = GetDesktopWindow();

EnableOpenGL(hwnd, &hDC, &hRC);

printf("OpenGL Version: %s
“, glGetString(GL_VERSION));
printf(” Vendor: %s
“, glGetString(GL_VENDOR));
printf(” Renderer: %s
", glGetString(GL_RENDERER));

glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pRGB);

FIBITMAP* image = FreeImage_ConvertFromRawBits(pRGB, width, height, 3*width, 24, 0xFF00FF, 0xFF00, 0x0000FF, false);
FreeImage_Save(FIF_JPEG, image, “ScreenShot-GLTest.jpg”, 0);

DisableOpenGL(hwnd, hDC, hRC);

Thanks for reading and hints!

Are you drawing anything? That can then be read back.

I’m getting the impression that you are getting the desktop DC and setting up GL on it. Do not do that.

Also, GL should not be used to take a snapshot of your desktop. That is not what it is for.

Yup, V-man, you are correct!

Thanks for the hint, so I gotta stay with windows api :-/