|
Links of Interest
MainDownload Prerequisites Screenshots Articles/Code Snippets DBP Warmup Entries Blog Contact/Feedback Quick Downloads:
Diaspora
XNA Requirements Installer
External Links
Heather [layout designer]XNA |
Screenshot Component
class ScreenshotComponent : GameComponent
{ public ScreenshotComponent(Game game) : base(game) { KeyboardHelper.OnKeyPressed += new KeyboardHelper.KeyEventHandler(KeyboardHelper_OnKeyPressed); } void KeyboardHelper_OnKeyPressed(Keys key) { if (key == Keys.F12) TakeScreenshot(); } void TakeScreenshot() { // Get the graphics device service IGraphicsDeviceService graphicsDeviceService = (IGraphicsDeviceService)Game.Services.GetService(typeof(IGraphicsDeviceService)); // Prepare a texture to save the screenshot into Texture2D ss = new Texture2D(graphicsDeviceService.GraphicsDevice, graphicsDeviceService.GraphicsDevice.DisplayMode.Width, graphicsDeviceService.GraphicsDevice.DisplayMode.Height, 1, ResourceUsage.ResolveTarget, graphicsDeviceService.GraphicsDevice.DisplayMode.Format, ResourceManagementMode.Manual); // Resolve the back buffer into the screenshot graphicsDeviceService.GraphicsDevice.ResolveBackBuffer(ss); // Find the next unused screenshot file in the format ssxxx.jpg, where xxx is a three digit number DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory()); if (dir.GetDirectories("ss").Length > 0) dir = dir.GetDirectories("ss")[0]; else dir = Directory.CreateDirectory("ss"); FileInfo[] files = dir.GetFiles("ss*.jpg", SearchOption.TopDirectoryOnly); int highestSS = 1; if (files.Length > 0) { string[] splits = files[files.Length - 1].Name.Split(new string[] { "ss", "." }, StringSplitOptions.RemoveEmptyEntries); highestSS = int.Parse(splits[0]); } highestSS++; // save the screenshot in the format ssxxx.jpg where xxx is a three digit number if (highestSS < 10) ss.Save("ss\\ss00" + highestSS + ".jpg", ImageFileFormat.Jpg); else if (highestSS < 100) ss.Save("ss\\ss0" + highestSS + ".jpg", ImageFileFormat.Jpg); else ss.Save("ss\\ss" + highestSS + ".jpg", ImageFileFormat.Jpg); // dispose the screenshot texture ss.Dispose(); } } |