- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/NewGamePhysics/Utilities/Screenshot.cs

    00001 ﻿// --------------------------------------------------------
    00002 // Screenshot.cs
    00003 // (A. Schiffler, 2010)
    00004 // --------------------------------------------------------
    00005
    00006 namespace NewGamePhysics.Utilities
    00007 {
    00008     using System;
    00009     using System.IO;
    00010     using System.Threading;
    00011
    00012     using Microsoft.Xna.Framework.Graphics;
    00013
    00017     public class Screenshot
    00018     {
    00022         private ResolveTexture2D screenshotTexture;
    00023
    00027         private string screenshotPath;
    00028
    00032         private static int screenshotNumber = 0;
    00033
    00037         private static AutoResetEvent fileSaved = new AutoResetEvent(true);
    00038
    00043         public Screenshot()
    00044         {
    00045             screenshotPath = System.Windows.Forms.Application.StartupPath + "\\Screenshots";
    00046             if (!System.IO.Directory.Exists(screenshotPath))
    00047             {
    00048                 System.IO.Directory.CreateDirectory(screenshotPath);
    00049             }
    00050         }
    00051
    00056         public void TakeScreenshot(GraphicsDevice graphiceDevice)
    00057         {
    00058             if (fileSaved.WaitOne(1000))
    00059             {
    00060                 fileSaved.Reset();
    00061                 int w = graphiceDevice.PresentationParameters.BackBufferWidth;
    00062                 int h = graphiceDevice.PresentationParameters.BackBufferHeight;
    00063                 screenshotTexture = new ResolveTexture2D(graphiceDevice, w, h, 1, SurfaceFormat.Bgr32);
    00064                 graphiceDevice.ResolveBackBuffer(screenshotTexture);
    00065                 Thread newThread = new Thread(ScreenshotThread);
    00066                 newThread.Start();
    00067             }
    00068         }
    00069
    00073         private void ScreenshotThread()
    00074         {
    00075             try
    00076             {
    00077                 if (!screenshotTexture.IsDisposed)
    00078                 {
    00079                     string filename = this.ScreenshotFilename("image");
    00080                     screenshotTexture.Save(filename, ImageFileFormat.Png);
    00081                     screenshotTexture.Dispose();
    00082                 }
    00083             }
    00084             finally
    00085             {
    00086                 fileSaved.Set();
    00087             }
    00088         }
    00089
    00095         private string ScreenshotFilename(string name)
    00096         {
    00097             string filename;
    00098
    00099             do
    00100             {
    00101                 screenshotNumber++;
    00102                 filename = String.Format(@"{0}\{1}_{2}.png", this.screenshotPath, name, screenshotNumber);
    00103             } while (File.Exists(filename));
    00104
    00105             return filename;
    00106         }
    00107     }
    00108 }