Taking screenshots from DirectX games | Newbie | | Join Date: Jul 2009
Posts: 10
| | |
Hi
I need to capture screenshots from DirectX games. I am using this code but sometimes I get totally blank images or the image of the desktop (but the game is open)
int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
bmpScreenShot.Save("test.jpg", ImageFormat.Jpeg);
There is actually no problem on XP, sometimes on Vista I get blank screen and on Windows 7 I get the view of desktop all the time but the game is on foreground. I see the game as a little black box at upper left corner on Windows 7.
I really need help.
Thanks.
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,783
| | | re: Taking screenshots from DirectX games
Do you just need screen shots?
Or do you need to take screen shots from within your own application?
It may just be easier to use something like "SnagIt" which can take shots of DirectX applications - than to re-invent the wheel
| | Newbie | | Join Date: Jul 2009
Posts: 10
| | | re: Taking screenshots from DirectX games Quote:
Originally Posted by tlhintoq Do you just need screen shots?
Or do you need to take screen shots from within your own application?
It may just be easier to use something like "SnagIt" which can take shots of DirectX applications - than to re-invent the wheel I need my application to take screenshots from DirectX applications.
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,783
| | | re: Taking screenshots from DirectX games Quote:
There is actually no problem on XP, sometimes on Vista I get blank screen and on Windows 7 I get the view of desktop all the time but the game is on foreground.
You need to dig into how DirectX actually works. It doesn't draw to the screen memory. That's why you are capturing the desktop, because the desktop is what is in Windows and in that memory space.
DirectX makes use of the video card memory and hardware acceleration.
In short, you need to learn about DirectX before you are going to be able to capture it.
| | Newbie | | Join Date: Jul 2009
Posts: 10
| | | re: Taking screenshots from DirectX games Quote:
Originally Posted by tlhintoq You need to dig into how DirectX actually works. It doesn't draw to the screen memory. That's why you are capturing the desktop, because the desktop is what is in Windows and in that memory space.
DirectX makes use of the video card memory and hardware acceleration.
In short, you need to learn about DirectX before you are going to be able to capture it. Thank you for your answer first of all. I still don't understand why it was ok when I was on XP and Vista. On Vista I sometimes got black screen but on Win7 I just got desktop.
I will look into how DirectX works but I really don't have any idea how to capture from an external DirectX application. Isn't there a simple code just to take screenshot from DX apps? Because I won't need to do anything with DX in future.
Is this the thing I am looking for? http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx |  | Site Moderator | | Join Date: Jan 2007 Location: America
Posts: 3,393
| | | re: Taking screenshots from DirectX games
That link looks good. Have you tried it?
| | Newbie | | Join Date: Jul 2009
Posts: 10
| | | re: Taking screenshots from DirectX games Quote:
Originally Posted by RedSon That link looks good. Have you tried it? I didn't actually understood how to use it? What's the device and how can I set it according to the DX app I want to capture from.
| | Familiar Sight | | Join Date: Jul 2009 Location: Calgary, Alberta, Canada
Posts: 233
| | | re: Taking screenshots from DirectX games
I only know a bit of XNA, but the device is usually a reference to an object that interacts with the graphics device. In XNA it comes as a member variable to the main Game class... perhaps it's similar for DirectX?
I don't know how similar they are though.
| | Newbie | | Join Date: Jul 2009
Posts: 10
| | | re: Taking screenshots from DirectX games Quote:
Originally Posted by GaryTexmo I only know a bit of XNA, but the device is usually a reference to an object that interacts with the graphics device. In XNA it comes as a member variable to the main Game class... perhaps it's similar for DirectX?
I don't know how similar they are though. Thanks for your help but of course device doesn't exist in my application because I am not taking screenshot of my own DX app. I am taking screenshots from an external DX app.
| | Newbie | | Join Date: Jul 2009
Posts: 10
| | | re: Taking screenshots from DirectX games
I have successfully managed to compile this code but now when I run this code I get "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." I think capturing from an external application is not possible with this method. I think I need another method.
Process[] procs = Process.GetProcessesByName("THE APP I WANT TO CAPTURE FROM");
IntPtr hWnd = IntPtr.Zero;
foreach (Process prc in procs)
{
if (prc.MainWindowHandle != IntPtr.Zero)
{
hWnd = prc.MainWindowHandle;
break;
}
}
Device device = new Device(hWnd);
Surface backbuffer = device.GetBackBuffer(0, 0, BackBufferType.Mono);
SurfaceLoader.Save("Screenshot.bmp", ImageFileFormat.Bmp, backbuffer);
backbuffer.Dispose();
| | Familiar Sight | | Join Date: Jul 2009 Location: Calgary, Alberta, Canada
Posts: 233
| | | re: Taking screenshots from DirectX games | | Newbie | | Join Date: Jul 2009
Posts: 10
| | | re: Taking screenshots from DirectX games Nope, still the same black screen or the view of desktop.
| | Newbie | | Join Date: Jul 2009
Posts: 10
| | | re: Taking screenshots from DirectX games
When I disable Aero with this method I can take screenshots successfully. But is there any way of doing this without disabling aero?
public readonly uint DWM_EC_DISABLECOMPOSITION = 0;
public readonly uint DWM_EC_ENABLECOMPOSITION = 1;
[DllImport("dwmapi.dll", EntryPoint = "DwmEnableComposition")]
protected static extern uint Win32DwmEnableComposition(uint uCompositionAction);
public void Aero(bool a)
{
if (a)
Win32DwmEnableComposition(DWM_EC_ENABLECOMPOSITION );
if (!a)
Win32DwmEnableComposition(DWM_EC_DISABLECOMPOSITIO N);
}
| | Newbie | | Join Date: Aug 2009
Posts: 2
| | | re: Taking screenshots from DirectX games
I have the exact same issue and I cannot figure it out. Could you post the full code you got working in vista with aero off?
| | Newbie | | Join Date: Jul 2009
Posts: 10
| | | re: Taking screenshots from DirectX games
Sure.
Just add using System.Runtime.InteropServices to your usings and add this to your code.
public readonly uint DWM_EC_DISABLECOMPOSITION = 0;
public readonly uint DWM_EC_ENABLECOMPOSITION = 1;
[DllImport("dwmapi.dll", EntryPoint = "DwmEnableComposition")]
protected static extern uint Win32DwmEnableComposition(uint uCompositionAction);
public void ManageAero(bool a)
{
if (a)
Win32DwmEnableComposition(DWM_EC_ENABLECOMPOSITION );
if (!a)
Win32DwmEnableComposition(DWM_EC_DISABLECOMPOSITIO N);
}
ManageAero(true) enables aero and ManageAero(false) disables it.
You may want to use ManageAero(true or false) in a try/catch block because when you are on XP or a prior Windows which doesnt have Desktop Window Manager it throws and exception.
| | Newbie | | Join Date: Aug 2009
Posts: 2
| | | re: Taking screenshots from DirectX games
Thanks! Ok, so lets say I start my screenshot app, with this I can then disable aero through code, then start d3d app, and take full screen shots of the app without the black screens?? When I close my d3d app, can I then use this function to turn aero back on?
| | Newbie | | Join Date: Jul 2009
Posts: 10
| | | re: Taking screenshots from DirectX games
Yes, you may get black screens rarely, but really rare.
You're welcome.
|  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|