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

Screenshot capture samples

Last post 05-13-2008 3:35 PM by Adam Miles. 9 replies.
  • 05-11-2008 10:51 PM

    Screenshot capture samples

    Does anyone have any sample code on how to output the xna window version to an image file (jpeg, bitmap, etc.)?  I need to screen capture the window in the draw function.

    Thanks. 

     

  • 05-11-2008 10:59 PM In reply to

    Re: Screenshot capture samples

    This thread has a sample http://forums.xna.com/thread/23619.aspx

    The ZBuffer News and information for XNA
    Please read the forum FAQs - Bug reporting
  • 05-12-2008 3:01 AM In reply to

    Re: Screenshot capture samples

    My problem is i have 4 different camera perspectives in my game.  So i divided my viewport into 4 squares (topleft, topright, bottomleft, bottomright).  Most samples I see are using ResolveBackBuffer which gets me the whole backbuffer.  How can I get each of the 4 views?  Anyone have an easy way to do that?

  • 05-12-2008 9:50 AM In reply to

    Re: Screenshot capture samples

    Not directly... you could rdraw one of the viewports to a rendertarget and then just grab the backbuffer of that. Probably easier to use System.Drawing methods to crop the image after you have it as a bitmap.

    The ZBuffer News and information for XNA
    Please read the forum FAQs - Bug reporting
  • 05-13-2008 1:41 AM In reply to

    Re: Screenshot capture samples

    Do you know how to convert the backbuffer texture to an byte[] image array?  I want to store it as an image datatype in sql server.  This was what i had.  I am not sure if its working right (the sample code i found was using a file as input).  I need to be able to eventually read the image datatype and display it on a picturebox too.

     

    ResolveTexture2D screenshot = new ResolveTexture2D(graphics.GraphicsDevice, width, height, 0, SurfaceFormat.Color);

    graphics.GraphicsDevice.ResolveBackBuffer(screenshot);

    byte[] image = null;

    image = System.Text.ASCIIEncoding.ASCII.GetBytes(screenshot.ToString());

     

  • 05-13-2008 1:56 AM In reply to

    Re: Screenshot capture samples

    I don't see how that could possibly work. Doing a ToString on the texture doesn't give you a dump of data bytes. And then putting it through ASCII encoding (text encoding) wouldn't give you anything of value.

    What you want to use is the GetData method on the screenshot like this:

    byte[] pixels = new byte[screenshot.Width * screenshot.Height];

    screenshot.GetData<byte>(pixels);


    Nick Gravelyn -- Microsoft XNA MVP
    Blog | The Best Game. Ever. | Next-Gen
  • 05-13-2008 2:03 PM In reply to

    Re: Screenshot capture samples

    Whenever I try to call the getdata function, I keep getting an error "The size of the data passed in is too large or too small for this resource."  I posted a quick test i did.

    What am I doing wrong? 


    using System;

    using System.Collections.Generic;

    using Microsoft.Xna.Framework;

    using Microsoft.Xna.Framework.Audio;

    using Microsoft.Xna.Framework.Content;

    using Microsoft.Xna.Framework.GamerServices;

    using Microsoft.Xna.Framework.Graphics;

    using Microsoft.Xna.Framework.Input;

    using Microsoft.Xna.Framework.Net;

    using Microsoft.Xna.Framework.Storage;

    namespace WindowsGameTest
    {

    public class Game1 : Microsoft.Xna.Framework.Game
    {

    GraphicsDeviceManager graphics;

    SpriteBatch spriteBatch;

    int width = 400;

    int height = 300;

    public Game1()
    {

    graphics = new GraphicsDeviceManager(this);

    Content.RootDirectory = "Content";

    this.graphics.PreferredBackBufferWidth = width;

    this.graphics.PreferredBackBufferHeight = height;

    }

    protected override void Initialize() { base.Initialize(); }

    protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); }



    protected override void UnloadContent() { }

    protected override void Update(GameTime gameTime) { base.Update(gameTime); }

    protected override void Draw(GameTime gameTime)
    {

    ResolveTexture2D screenshot = new ResolveTexture2D(graphics.GraphicsDevice, width, height, 1, SurfaceFormat.Color);

    graphics.GraphicsDevice.ResolveBackBuffer(screenshot);

    byte[] image = new byte[screenshot.Width * screenshot.Height];

    screenshot.GetData<byte>(image);

    graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

    base.Draw(gameTime);

    /*

    SqlCommand command;

    command = new SqlCommand("INSERT INTO Videos (timestamp,cameraID, image) VALUES (GETDATE(),4, @image)", conn);

    //command.Parameters.Add("@datetime", SqlDbType.DateTime).Value = DateTime.Now;

    command.Parameters.Add("@image", SqlDbType.Image).Value = image;

    try

    {

    int i = command.ExecuteNonQuery();

    }

    catch

    {

    int i = 0;

    }

    */


    }

    }

    }

  • 05-13-2008 2:18 PM In reply to

    Re: Screenshot capture samples

    Please use the insert code button when you want to include code in a forum post: that makes the formatting legible.

    A byte is 8 bits. Your resolve target uses SurfaceFormat.Color, which is 32 bits.

    You need to either include 4 times as many bytes in your array, or use an array of Color structures instead of bytes.
    XNA Framework Developer - blog - homepage
  • 05-13-2008 3:02 PM In reply to

    Re: Screenshot capture samples

    Sorry about that.  Thanks for formatting so it looks legible.

    The instant i uncomment the sql code part, add in a open sqlconnection in the intialize function...and try to insert the screenshot into the database on localhost...

    my framerate just goes down from a consistent 60/sec to like like 5-10/sec. How can i keep the consistent 60 frames a second (even a consistent 30 would be ok).  I've tested the database and i can insert over 200 jpeg images a second so that's not the problem.  Is there something going on when i try and convert the byte[] to sql image datatype?  Or should i be using a different sql datatype to store images so i can keep the framerate performance?

    Thanks so much for the help.

  • 05-13-2008 3:35 PM In reply to

    Re: Screenshot capture samples

    It runs at 10/second because you're commiting the age old gotcha of resolving the back buffer and calling 'GetData' on it every single frame. Shawn has probably done this one to death, the explanation involves the words "stalling" and "synchronising" and ends up with the suggestion you just don't do it.

    It's fine for one-off screenshots, but you're trying to record a video by sending the entire backbuffer, uncompressed, back to the CPU at a rate of 60 times per second and that just is not doable.

Page 1 of 1 (10 items) Previous Next