How to make screenshot using C# & XNA?
Asked Answered
A

3

6


How to make screenshot and save it to HDD using C# & XNA, while running game in fullscreen mode?

Avraham answered 3/4, 2011 at 13:19 Comment(3)
Duplicate of https://mcmap.net/q/1771551/-take-screen-shot-in-xnaMasquerade
But, I want to make it while running and I don't have anything like 'ResolveTexture2D', and save it, not print.Avraham
This question is not a duplicate (of that one anyway). The API changed in XNA 4.0 and the answer there is not applicable.Blodget
B
8

The API was changed in XNA 4.0.

If you are running on the HiDef profile (Xbox 360 and newer Windows machines), you can use GraphicsDevice.GetBackBufferData.

To make saving that data easy, you could use put the output from that into a Texture2D.SetData and then use SaveAsPng or SaveAsJpeg (this is slightly slower than it needs to be, because it also sends the data back to the GPU - but it is just so easy).

If you are using the Reach profile, then you have to render your scene to a RenderTarget2D. My answer here should give you a good starting point.

Blodget answered 3/4, 2011 at 15:21 Comment(2)
How to make that if I am using GraphicsDeviceManager? ( I'm beginner, sorry for such a stupid question :) )Avraham
I don't quite understand your question. Are you looking for GraphicsDeviceManager.GraphicsDevice? You should check MSDN for these things. Of course, if you are in a class derived from Game or GameComponent, they have GraphicsDevice properties you can access directly.Blodget
W
4

Here take 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();
Wilmott answered 9/11, 2011 at 11:0 Comment(0)
H
0

This answer shows you how to take a screenshot. In this example, it is saving an image every render, so you just need to move it to a function that you can call when you want to save the screenshot.

Haleyhalf answered 3/4, 2011 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.