Home arrow TUTORIALS arrow How to take screenshots in your game
How to take screenshots in your game PDF Print E-mail
Written by Mark Thomas   
Wednesday, January 17 2007
Article Index
How to take screenshots in your game
Page 2

One handy feature in a lot of games is the ability to take screenshots. This tutorial walks you through the process of doing just that, using a component that you can download at the end of the tutorial. IMPORTANT: This only works for Windows games.

Before we get started, I'll say this again- this only works for Windows games. The component uses code that collects the startup path of the application. My testing has shown no problems with it, but your mileage may vary. Hot Seat, Ltd is not liable for any problems that may occur as a result of you using this code.

I've created a simple game library called hsScreenshot. As of this writing, it only contains one method, TakeScreenshot(). Before we get into the creation of the component, let's take a look at just how easy it is to use. All you'll need is a functioning XNA game. I'll be using the tutorial game that I modified in my first tutorial that covered keyboard and mouse input.

Add a Reference

After opening your project, add a reference to hsScreenshot.dll, which you can download at the end of the tutorial. To add a reference, right-click on the project name in the Solution Explorer. Choose Add Reference... and select the Browse tab. Navigate to hsScreenshot.dll and click OK. 

 Now we need to set up code that will cause a screenshot to be taken. I want to do this when the user presses the F12 key. The tutorial game uses a routine called UpdateInput() to collect input. We just need to add a check for the F12 key, like so:

//get the keyboard state
   KeyboardState curKbState = Keyboard.GetState();
//other input collection code removed for brevity
   //see if we want to take a screenshot
   if (curKbState.IsKeyDown(Keys.F12))
    hs.Screenshot screenshot = new hs.Screenshot(graphics);
    System.Diagnostics.Trace.WriteLine(screenshot.TakeScreenshot("myScreen",".tga", ImageFileFormat.Tga));

We instantiate an instance of our Screenshot object, sending our GraphicsDeviceManager to the constructor. Note that if you intend to take a lot of screenshots in your game, it may be advantageous to declare a Screenshot object that won't be created and destroyed every time. Also note that if the user holds the F12 key down, many screenshots will be generated. The code automatically increments the filename if the file already exists.

The TakeScreenshot() method has two overloads. If you call it with no parameters, you will get a jpg file called screen.jpg. Alternatively you can specify a filename, extension, and a file format. The method returns a string that, upon success, contains a message indicating the file was created, along with the filename. If there is an error it will return the Exception message. The screenshot(s) will be located in the folder in which your executable is located.

That's all there is to using this component! Now let's see how it's made.

Last Updated ( Thursday, January 18 2007 )
< Prev   Next >
Hot Seat, Ltd - Consulting and Games
Web hosting services