xna中的截屏操作处理
游戏中常会用到截图,如果是用默认的截屏键PrintScreen的话,用户又要在另外的地方粘贴才可以。
这样做的话比较自由:
1 /// <summary>
2 /// Saves a screenshot to disk with running number.
3 /// Does not overwrite existing screenshots.
4 /// </summary>
5 public void SaveScreenshot() {
6 // Find a free name
7 int number = 0;
8 string filename = String.Format("screenshot{0:00}.png", number);
9 while (System.IO.File.Exists(filename)) {
10 filename = String.Format("screenshot{0:00}.png", ++number);
11 }
12
13 // Take the screenshot
14 GraphicsDevice device = graphics.GraphicsDevice;
15 int w = device.PresentationParameters.BackBufferWidth;
16 int h = device.PresentationParameters.BackBufferHeight;
17 using (ResolveTexture2D screenshot = new ResolveTexture2D(device, w, h, 1, SurfaceFormat.Color)) {
18 // Grab the screenshot
19 device.ResolveBackBuffer(screenshot);
20
21 // Set the alpha to full
22 Color[] data = new Color[screenshot.Width * screenshot.Height];
23 screenshot.GetData<Color>(data);
24 int pos = 0;
25 foreach (Color c in data) {
26 data[pos++] = new Color(c.R, c.G, c.B, 255);
27 }
28
29 // Write to disk
30 screenshot.SetData<Color>(data);
31 screenshot.Save(filename, ImageFileFormat.Png);
32 screenshot.Dispose();
33 }
34 }
2 /// Saves a screenshot to disk with running number.
3 /// Does not overwrite existing screenshots.
4 /// </summary>
5 public void SaveScreenshot() {
6 // Find a free name
7 int number = 0;
8 string filename = String.Format("screenshot{0:00}.png", number);
9 while (System.IO.File.Exists(filename)) {
10 filename = String.Format("screenshot{0:00}.png", ++number);
11 }
12
13 // Take the screenshot
14 GraphicsDevice device = graphics.GraphicsDevice;
15 int w = device.PresentationParameters.BackBufferWidth;
16 int h = device.PresentationParameters.BackBufferHeight;
17 using (ResolveTexture2D screenshot = new ResolveTexture2D(device, w, h, 1, SurfaceFormat.Color)) {
18 // Grab the screenshot
19 device.ResolveBackBuffer(screenshot);
20
21 // Set the alpha to full
22 Color[] data = new Color[screenshot.Width * screenshot.Height];
23 screenshot.GetData<Color>(data);
24 int pos = 0;
25 foreach (Color c in data) {
26 data[pos++] = new Color(c.R, c.G, c.B, 255);
27 }
28
29 // Write to disk
30 screenshot.SetData<Color>(data);
31 screenshot.Save(filename, ImageFileFormat.Png);
32 screenshot.Dispose();
33 }
34 }