繁体   English   中英

如何使用C#和XNA制作屏幕截图?

[英]How to make screenshot using C# & XNA?


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

在XNA 4.0更改了 API。

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

为了便于保存数据,您可以使用将其输出放入Texture2D.SetData然后使用SaveAsPngSaveAsJpeg (这比它需要的稍慢,因为它还将数据发送回GPU - 但它真是太容易了

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

这里看看这段代码。

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();

这个答案向您展示了如何拍摄截图。 在此示例中,它是每次渲染时保存图像,因此您只需将其移动到可以在保存屏幕截图时调用的函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM