1

I'm trying to take screenshots of a game created in OpenGL..

I have this code:

_captureProcess.BringProcessWindowToFront();
ScreenCapture sc = new ScreenCapture();
Image img = sc.CaptureWindow(_process.MainWindowHandle);

From: http://www.developerfusion.com/code/4630/capture-a-screen-shot/

The IntPTR is correct! But the image is completely black!

_process = Game in full screen, OPENGL..

How could I do to be able to take screenshots in real time, without getting the black screen?

Charles
  • 50,943
  • 13
  • 104
  • 142
  • Are you on Windows? In fullscreen mode starting with Windows Vista, there is trouble with anything that tries to capture the front-buffer (including the built-in Alt + PrintScreen). The easiest solution is to change your buffer swap behavior to Copy Swap (`PFD_SWAP_COPY`, slower but guaranteed to work). Often if you Alt+TAB out and back in after making the fullscreen mode switch that will fix it too; though I have never been able to explain that ;) If you did not write the game in the question, then the second solution may be your only choice if you want to use that code. – Andon M. Coleman Jan 26 '14 at 00:39

1 Answers1

2

The code sample you linked depends on a undefined behavior one could find in Windows up until and including Windows XP: If you created, or moved a window to the top and prevented it from redrawing, it would receive the contents of everything beneath it. This behavior has nevern been specified but a lot of people exploited it.

Windows Vista introduced compositing, where each window lives in its own off-screen memory area and doesn't interact with the other windows. Hence all you see in such a window in the black of the cleared off-screen buffer.

How could I do to be able to take screenshots in real time, without getting the black screen?

By using the proper APIs provided by Windows, namely the DWM API for taking screenshots in Vista and later.

https://msdn.microsoft.com/en-us/library/windows/desktop/aa969540(v=vs.85).aspx

datenwolf
  • 159,371
  • 13
  • 185
  • 298