C# Capturing A Fullscreen Game With SlimDX

2019-04-14 02:57发布

问题:

I have recently made an application that takes screenshots of games.

First I used GDI to capture the game but for that I had to disable aero at win7/vis and I had some issues on windows XP.

Than I decided going with DirectX and after realizing that microsoft's DX isn't going to work for me, I found this great example of a SlimDX screen capture (http://spazzarama.wordpress.com/2009/02/07/screencapture-with-direct3d/)

SlimDX worked GREAT! and it even allowed me to capture a game without having to disable aero on win7/vis.

Only problem is - when I switch the game to fullscreen I get "D3DERR_DEVICELOST: Device lost (-2005530520)".

Can anyone suggest a way to customize the code so it'll capture fullscreen application?

NOTE : I already have a method that can determine if the game is fullscreen or windowed.

And I change this line :

bitmap = new Bitmap(SlimDX.Direct3D9.Surface.ToStream(surface, SlimDX.Direct3D9.ImageFileFormat.Bmp, new Rectangle(region.Left, region.Top, region.Width, region.Height)));

to this :

bitmap = new Bitmap(SlimDX.Direct3D9.Surface.ToStream(surface, SlimDX.Direct3D9.ImageFileFormat.Bmp));

回答1:

When a Direct3D device enters fullscreen mode, it's granted exclusive access to the video card. From the MSDN documentation:

when a Direct3D object assumes exclusive mode, all devices other than the one that went full-screen are placed in lost state.

I don't think there's any way to make your technique work for taking screenshots of another application after it goes into fullscreen exclusive mode. My understanding is that other applications which retrieve the state of fullscreen surfaces (i.e. video recording programs and the like) do so by injecting their own code into the Direct3D libraries themselves. This is a non-trivial endeavor; you can try searching for "directx code injection" or something similar if you're interested in going down that road.