473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Taking screenshots from DirectX games

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.
Jul 29 '09 #1
16 18036
tlhintoq
3,525 Expert 2GB
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
Jul 29 '09 #2
@tlhintoq
I need my application to take screenshots from DirectX applications.
Jul 29 '09 #3
tlhintoq
3,525 Expert 2GB
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.
Jul 29 '09 #4
@tlhintoq
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
Jul 29 '09 #5
RedSon
5,000 Expert 4TB
That link looks good. Have you tried it?
Jul 29 '09 #6
@RedSon
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.
Jul 29 '09 #7
GaryTexmo
1,501 Expert 1GB
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.
Jul 30 '09 #8
@GaryTexmo
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.
Jul 30 '09 #9
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();
Jul 30 '09 #10
GaryTexmo
1,501 Expert 1GB
Does this article help?
http://spazzarama.wordpress.com/2009...with-direct3d/
Jul 30 '09 #11
@GaryTexmo
Nope, still the same black screen or the view of desktop.
Jul 30 '09 #12
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);
}
Jul 30 '09 #13
v8maro
2
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?
Aug 12 '09 #14
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.
Aug 12 '09 #15
v8maro
2
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?
Aug 12 '09 #16
Yes, you may get black screens rarely, but really rare.

You're welcome.
Aug 12 '09 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Akkadian123 | last post by:
Hi guys, I'm a student of c#, and I was wondering how one would take advantage of multimedia in most c# apps/games. I assume one would use directx9, since there doesn't seem to be any support...
2
by: JessCurious | last post by:
1. does anybody have a discussion of pros and cons directx9 vs GDI+? is GDI+ simply last year's version and directx9 the future, or do they each have advantages? 2. does directx9 do...
6
by: Tim Bücker | last post by:
Hello. What is the best to use? DirectDraw from DirectX 7.0 or Direct3D from DirectX 8.0 when the only thing one wants to do is to draw bitmaps fast on the screen in fullscreen mode. I think...
3
by: johnb41 | last post by:
I am building a .net app that does some simple image tasks (Tiff files): viewing Tiff, viewing thumbnails from multipage tiff, rotation, page (frame) deletion, etc. The problem is that it is so...
4
by: poldoj | last post by:
Hi all ( and happy new year of course) I just would like to learn how to use directx in my VB.net project. Simple stuff, Im only a newbie, someone can point me out a link for tutorials? Thanks
0
by: odie5533 | last post by:
I am looking to make a program similar to Xfire which will overlay information while playing a directx game. For an example of what xfire is capable of, see the image below:...
9
by: gillian keenan | last post by:
my son has pc games with directx on them, when we install it it says that we need to install directx so when i have tried to install it a message comes up saying it has already been installed. when...
2
by: iwan84 | last post by:
I insalled a game and from that moment my version of directx is 4.09.00.0904. When I try to install direcx 9c it say components istalled are ready for use, but old version of directx is default...
11
by: raylopez99 | last post by:
I just downloaded the apparently free SDK for DirectX 9, all 200+ MB of it, but as I read about WPF I wonder: is DirectX obsolete? Should I even bother learning how to use it? After all, the end...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.