如何在全屏模式下运行游戏时使用C#和XNA进行屏幕截图并保存到硬盘?
GraphicsDeviceManager.GraphicsDevice
吗?你应该查看MSDN来了解这些内容。当然,如果你在从Game
或GameComponent
派生的类中,它们有GraphicsDevice
属性,你可以直接访问。 - Andrew Russell请看这段代码。
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();