Taking a screenshot in XNA 4.0

     由于XNA4.0和3.0 3.1很多不一样,保存屏幕的方法也不尽一样;下面的方法可以在4.0下进行截屏并保存。

public void ScreenShot(string prefix)
        {
#if WINDOWS
            int w = GraphicsDevice.PresentationParameters.BackBufferWidth;
            int h = GraphicsDevice.PresentationParameters.BackBufferHeight;

            //force a frame to be drawn (otherwise back buffer is empty)
            Draw(new GameTime());

            //pull the picture from the buffer
            int[] backBuffer = new int[w * h];
            GraphicsDevice.GetBackBufferData(backBuffer);

            //copy into a texture
            Texture2D texture = new Texture2D(GraphicsDevice, w, h, false, GraphicsDevice.PresentationParameters.BackBufferFormat);
            texture.SetData(backBuffer);

            //save to disk
            Stream stream = File.OpenWrite(prefix + "_" + Guid.NewGuid().ToString() + ".png");

            texture.SaveAsPng(stream, w, h);
            stream.Close();

#elif XBOX
    throw new NotSupportedException();
#endif
        }


http://clifton.me/screenshot-xna-csharp/

你可能感兴趣的:(Taking a screenshot in XNA 4.0)