How to take a screenshot using C # and XNA?


How to take a screenshot and save it to your hard drive using C # and XNA when starting the game in full screen mode?

+4
source share
3 answers

The API has been changed in XNA 4.0 .

If you work in a HiDef profile (Xbox 360 and later Windows machines), you can use GraphicsDevice.GetBackBufferData .

To make it easier to save this data, you can use the output from this in Texture2D.SetData and then use SaveAsPng or SaveAsJpeg (this is a bit slower than it should be, since it also sends data back to the GPU - but it's that simple).

If you are using a Reach profile, you need to make your scene. My answer here should give you a good starting point.

+6
source

Here is a look at this code.

 count += 1; string counter = count.ToString(); 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(counter + ".jpg"); texture.SaveAsJpeg(stream, w, h); stream.Dispose(); texture.Dispose(); 
+3
source

This answer shows you how to take a screenshot. In this example, it saves an image for each rendering, so you just need to move it to a function that you can call when you want to save a screenshot.

0
source

Source: https://habr.com/ru/post/1346440/


All Articles