View Full Version : Screenshot with OpenGL

02-21-2012, 12:52 PM

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(3*width*height);

hwnd = GetDesktopWindow();

EnableOpenGL(hwnd, &hDC, &hRC);

printf("OpenGL Version: %s\n", glGetString(GL_VERSION));
printf(" Vendor: %s\n", glGetString(GL_VENDOR));
printf(" Renderer: %s\n", 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!

02-21-2012, 01:03 PM
Are you drawing anything? That can then be read back.

02-21-2012, 01:12 PM
I'm getting the impression that you are getting the desktop DC and setting up GL on it. Do not do that.
http://www.opengl.org/wiki/Platform_specifics:_Windows#How_many_times_can_I_c all_SetPixelFormat.3F

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

02-22-2012, 12:19 AM
Yup, V-man, you are correct!

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