Take screenshot of a game using OpenGL with C#
I've been searching how to take a screenshot of a game that uses the full screen and also uses OpenGL. If I use either CopyFromScreen or BitBlt t开发者_StackOverflow社区he game screen won't appear and I will only see what it's behind the game. I've read some tutorials about this but they involve DLL hooking and some C++. Is there a way of doing this using purely C#? (Or with some invokes...)
BitBlt using the screen's HDC (pass NULL to the GetDC function) is your best bet unless you want to use a hook DLL. If that does not work, you have few options left.
glReadPixels will not work just like this. You need the application's GL context being the current context in your thread, otherwise you get a black image in the best, and a segmentation fault in the average case. wglShareLists or wglCopyContext and such won't help you either, because you can share a context only within the same process.
That's where the DLL hooking comes in. Basically, you make a DLL that will be placed in the program's folder, which exports the same symbols as the OpenGL DLL and simply forwards them all to the original (simply, huh!).
However, for SwapBuffers, you would do a little more than that. You would first forward the function to the original and then do your glReadPixels. As it happens, when that function is called, the GL context is necessarily current in your thread, so it will work. Also, since SwapBuffers has just done its work, you are guaranteed that all draw calls belonging to the previous frame have finished and a complete image is now in the front buffer.
精彩评论