XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

Taking a screenshot in XNA

Last post 12/3/2008 12:17 AM by The ZMan. 5 replies.
  • 7/6/2007 6:01 AM

    Taking a screenshot in XNA

    Hello,

    In my little project, I have a simple sprite on the XNA window which is load and drawn basically:

    protected override void LoadGraphicsContent(bool loadAllContent)
    {
    if (loadAllContent)
    {
    myTexture = content.Load<Texture2D>("globe");
    spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
    }
    }

    protected override void Draw(GameTime gameTime)
    {
    graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
    spriteBatch.Draw(myTexture, spritePosition, Color.White);
    spriteBatch.End();

    base.Draw(gameTime);
    }


    Now I am trying to write a method for taking a screenshot. Now it looks like:

    private void Screenshot()
    {
    using (Texture2D screenshot = new Texture2D(graphics.GraphicsDevice,
    graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
    graphics.GraphicsDevice.PresentationParameters.BackBufferHeight, 1,
    ResourceUsage.ResolveTarget,
    SurfaceFormat.Color,
    ResourceManagementMode.Manual))
    {
    graphics.GraphicsDevice.ResolveBackBuffer(screenshot);
    screenshot.Save("test.bmp", ImageFileFormat.Bmp);
    }
    }


    Here is the problem: this screenshot method takes the screenshot of all graphichs which is loaded, including 2D sprites and 3D meshes. I don't want any sprites in the screenshot because these sprites are my UI elements.

    So how to take the screenshot where all 2D sprites are excluded?

    Thanks in advance.
  • 7/6/2007 6:15 AM In reply to

    Re: Taking a screenshot in XNA

    Answer
    Reply Quote

    I think probably the easiest thing to do would be to use a bool variable to track whether the UI should be displayed, then perform the following steps:

    1. From within your Screenshot() method, set the UI bool variable to false.
    2. Call your game's Draw() method, drawing the scene without the UI.
    3. Take the screenshot.
    4. Set the UI variable back to true and call Draw() again.
  • 7/6/2007 7:38 PM In reply to

    Re: Taking a screenshot in XNA

    To cut down on the number of Draw() calls, I would instead draw all 3D objects, then take a screenshot if needed, then finally draw your UI stuff.

    I don't know if it will like having a ResolveBackBuffer() call in the middle of drawing everything or not though...

    Actually, reading back through, I think that is pretty much what was described above :S
  • 7/6/2007 7:57 PM In reply to

    Re: Taking a screenshot in XNA

    That's a good thought, and it might be better to execute the Screenshot() call from inside the Draw() method anyway (after drawing the scene but before drawing the UI) because after thinking about it, I'm not sure if it would work to manually call Draw() because the Game class may be setting various render state information before it automatically calls Draw(), and if you manually call Draw(), this render state information would not be set.

  • 12/3/2008 12:14 AM In reply to

    Re: Taking a screenshot in XNA

    After looking into this, I've found a way quicker method that requires no code. You can just use the XNA Game Studio Device Manager in Visual Studio to take pictures when debugging on the 360. It'll take a picture of what's actually on the Xbox, which is handy if you've got a multiplayer game. I've posted a walkthrough here.
  • 12/3/2008 12:17 AM In reply to

    Re: Taking a screenshot in XNA

    Not that this thread is very old. Back when it was posted the code was the only way to do this.

    Note that using the device manager also works on the Zune.
    Play Kissy Poo - a game for 4 year olds on Xbox and windows
    The ZBuffer
    News and information for XNA
      Follow The Zman on twitter, Email me
        Please read the forum FAQs - Bug/Feature reporting
          Don't forget to mark good answers and good playtest feedback when you see it!!!
Page 1 of 1 (6 items) Previous Next