5


在全屏模式下运行游戏时,如何使用 C# 和 XNA 制作屏幕截图并将其保存到硬盘?

4

3 回答 3

7

XNA 4.0中更改了 API 。

如果您在HiDef配置文件(Xbox 360 和更新的 Windows 机器)上运行,您可以使用GraphicsDevice.GetBackBufferData.

为了使保存该数据变得容易,您可以使用将输出放入 aTexture2D.SetData然后使用SaveAsPngor SaveAsJpeg(这比它需要的稍慢,因为它还将数据发送回 GPU - 但它就是这么简单) .

如果您使用的是Reach配置文件,那么您必须将场景渲染为RenderTarget2D. 我在这里的回答应该给你一个很好的起点。

于 2011-04-03T15:21:28.810 回答
4

下面看看这段代码。

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();
于 2011-11-09T11:00:47.937 回答
0

这个答案向您展示了如何截取屏幕截图。在这个例子中,它会在每次渲染时保存一个图像,所以你只需要将它移动到一个你可以在想要保存屏幕截图时调用的函数中。

于 2011-04-03T14:54:45.817 回答