6


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

Neomex
  • 1,650
  • 6
  • 24
  • 38
  • Duplicate of http://stackoverflow.com/questions/1775196/take-screen-shot-in-xna – Brook Apr 03 '11 at 13:24
  • But, I want to make it while running and I don't have anything like 'ResolveTexture2D', and save it, not print. – Neomex Apr 03 '11 at 13:40
  • This question is **not a duplicate** (of that one anyway). The API changed in XNA 4.0 and the answer there is not applicable. – Andrew Russell Apr 03 '11 at 15:11

3 Answers3

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.

Community
  • 1
  • 1
Andrew Russell
  • 26,924
  • 7
  • 58
  • 104
  • How to make that if I am using GraphicsDeviceManager? ( I'm beginner, sorry for such a stupid question :) ) – Neomex Apr 03 '11 at 16:22
  • I don't quite understand your question. Are you looking for `GraphicsDeviceManager.GraphicsDevice`? You should [check MSDN](http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphicsdevicemanager_members.aspx) for these things. Of course, if you are in a class derived from `Game` or `GameComponent`, they have [`GraphicsDevice` properties](http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game_members.aspx) you can access directly. – Andrew Russell Apr 04 '11 at 01:30
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();
Omar
  • 16,329
  • 10
  • 48
  • 66
Christoff
  • 83
  • 8
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.

Community
  • 1
  • 1
Neil Knight
  • 47,437
  • 25
  • 129
  • 188