How to make screenshot and save it to HDD using C# & XNA, while running game in fullscreen mode?
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.
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 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();
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.
© 2022 - 2024 — McMap. All rights reserved.