public void SaveScreenshot() {
// Find a free name
int number = 0;
string filename = String.Format("screenshot{0:00}.png", number);
while (System.IO.File.Exists(filename)) {
filename = String.Format("screenshot{0:00}.png", ++number);
}
// Take the screenshot
GraphicsDevice device = graphics.GraphicsDevice;
int w = device.PresentationParameters.BackBufferWidth;
int h = device.PresentationParameters.BackBufferHeight;
using (ResolveTexture2D screenshot = new ResolveTexture2D(device, w, h, 1, SurfaceFormat.Color)) {
// Grab the screenshot
device.ResolveBackBuffer(screenshot);
// Set the alpha to full
Color[] data = new Color[screenshot.Width * screenshot.Height];
screenshot.GetData<Color>(data);
int pos = 0;
foreach (Color c in data) {
data[pos++] = new Color(c.R, c.G, c.B, 255);
}
// Write to disk
screenshot.SetData<Color>(data);
screenshot.Save(filename, ImageFileFormat.Png);
screenshot.Dispose();
}
}
XNA 4.0不行~