February 11th, 2007 by Sharky

kick it on GameDevKicks.com 

The only way to know for sure is to try it, so sorry for the spam.

Here goes…

Here is some code formatted for the blog using this site

public void TakeScreenshot()
{
        IGraphicsDeviceService gs = (IGraphicsDeviceService)this.Services.GetService(typeof(IGraphicsDeviceService));
        GraphicsDevice device = gs.GraphicsDevice;

        int w = device.PresentationParameters.BackBufferWidth;
        int h = device.PresentationParameters.BackBufferHeight;

        using (Texture2D screenshot = new Texture2D(device,  w,  h,  1, 
                                                    ResourceUsage.ResolveTarget, 
                                                    SurfaceFormat.Color, 
                                                    ResourceManagementMode.Manual))
        {
            device.ResolveBackBuffer(screenshot);

            string fileName = "";

            if (_screenshotPath == null)
            {
                _screenshotPath = Application.StartupPath;

                if (!System.IO.Directory.Exists(_screenshotPath + "\\screenshots"))
                    System.IO.Directory.CreateDirectory(_screenshotPath + "\\screenshots");

                _screenshotPath += "\\screenshots";
            }
            else
            {
                //make sure path is valid
                if (!System.IO.Directory.Exists(_screenshotPath))
                {
                    try
                    {
                        System.IO.Directory.CreateDirectory(_screenshotPath);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }

            string timestampName;

            timestampName = DateTime.Now.ToString("yyyyMMdd_HHmmss");
            string[] files;
            files = Directory.GetFiles(_screenshotPath,  string.Format("{0}.*",  timestampName));

            if (files.Length > 0)
            {
                string tryName = timestampName;
                for (int seq = 0; files.Length > 0; seq++)
                {
                    tryName = String.Format("{0}_{1}",  timestampName,  seq);
                    files = Directory.GetFiles(_screenshotPath,  string.Format("{0}.*",  tryName));
                }

                timestampName = tryName;
            }

            fileName = String.Format("{0}\\{1}.bmp",  _screenshotPath,  timestampName);
            screenshot.Save(fileName,  ImageFileFormat.Bmp);
        }
}

Here is the same snippet formatted using a Live Writer plugin 

 

public void TakeScreenshot()
{
        IGraphicsDeviceService gs = (IGraphicsDeviceService)this.Services.GetService(typeof(IGraphicsDeviceService));
        GraphicsDevice device = gs.GraphicsDevice;
        int w = device.PresentationParameters.BackBufferWidth;
        int h = device.PresentationParameters.BackBufferHeight;
        using (Texture2D screenshot = new Texture2D(device, w, h, 1,
                                                    ResourceUsage.ResolveTarget,
                                                    SurfaceFormat.Color,
                                                    ResourceManagementMode.Manual))
        {
            device.ResolveBackBuffer(screenshot);
            string fileName = "";
            if (_screenshotPath == null)
            {
                _screenshotPath = Application.StartupPath;
                if (!System.IO.Directory.Exists(_screenshotPath + "\\screenshots"))
                    System.IO.Directory.CreateDirectory(_screenshotPath + "\\screenshots");
                _screenshotPath += "\\screenshots";
            }
            else
            {
                //make sure path is valid
                if (!System.IO.Directory.Exists(_screenshotPath))
                {
                    try
                    {
                        System.IO.Directory.CreateDirectory(_screenshotPath);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }
            string timestampName;
            timestampName = DateTime.Now.ToString("yyyyMMdd_HHmmss");
            string[] files;
            files = Directory.GetFiles(_screenshotPath, string.Format("{0}.*", timestampName));
            if (files.Length > 0)
            {
                string tryName = timestampName;
                for (int seq = 0; files.Length > 0; seq++)
                {
                    tryName = String.Format("{0}_{1}", timestampName, seq);
                    files = Directory.GetFiles(_screenshotPath, string.Format("{0}.*", tryName));
                }
                timestampName = tryName;
            }
            fileName = String.Format("{0}\\{1}.bmp", _screenshotPath, timestampName);
            screenshot.Save(fileName, ImageFileFormat.Bmp);
        }
}

Here’s the same from another Live Writer plugin “InsertFromVisualStudio

public void TakeScreenshot()
{
        IGraphicsDeviceService gs = (IGraphicsDeviceService)this.Services.GetService(typeof(IGraphicsDeviceService));
        GraphicsDevice device = gs.GraphicsDevice;

        int w = device.PresentationParameters.BackBufferWidth;
        int h = device.PresentationParameters.BackBufferHeight;

        using (Texture2D screenshot = new Texture2D(device,  w,  h,  1, 
                                                    ResourceUsage.ResolveTarget, 
                                                    SurfaceFormat.Color, 
                                                    ResourceManagementMode.Manual))
        {
            device.ResolveBackBuffer(screenshot);

            string fileName = "";

            if (_screenshotPath == null)
            {
                _screenshotPath = Application.StartupPath;

                if (!System.IO.Directory.Exists(_screenshotPath + "\\screenshots"))
                    System.IO.Directory.CreateDirectory(_screenshotPath + "\\screenshots");

                _screenshotPath += "\\screenshots";
            }
            else
            {
                //make sure path is valid
                if (!System.IO.Directory.Exists(_screenshotPath))
                {
                    try
                    {
                        System.IO.Directory.CreateDirectory(_screenshotPath);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }

            string timestampName;

            timestampName = DateTime.Now.ToString("yyyyMMdd_HHmmss");
            string[] files;
            files = Directory.GetFiles(_screenshotPath,  string.Format("{0}.*",  timestampName));

            if (files.Length > 0)
            {
                string tryName = timestampName;
                for (int seq = 0; files.Length > 0; seq++)
                {
                    tryName = String.Format("{0}_{1}",  timestampName,  seq);
                    files = Directory.GetFiles(_screenshotPath,  string.Format("{0}.*",  tryName));
                }

                timestampName = tryName;
            }

            fileName = String.Format("{0}\\{1}.bmp",  _screenshotPath,  timestampName);
            screenshot.Save(fileName,  ImageFileFormat.Bmp);
        }
}

Average Rating: 4.5 out of 5 based on 178 user reviews.

8 Responses to “”

  1. Ultrahead Says:

    Good to know this quite handy links … thanks!

  2. Sharky Says:

    hehe.

    Overall I think I’m favouring the last one (#3).

    #1 is fairly good, but it seemed to need a css Stylesheet hooked up to the Blog to render with any colouring.

    #3 is visually ok, and as a plugin it’s much nicer to use. After pasting into Live Writer it still allows edits, whereas #2 doesn’t.

    #3 seems pretty self contained too – no additional stylesheets required.

  3. dczraptor Says:

    Actually, with #2 you can still edit the code. Select the code block and then go over to the right side and click the ‘edit code’ button. You can even insert line numbers, a border, or change the background color. However, you won’t get the same amount of manual control over it, but it’s pretty close.

  4. TehOne Says:

    I would have to say I like #3 the best as well. #1 is good but it makes the page overflow very wide. #2 is nice but too condensed and wraps too much for my tastes. #3 is just right i’d say, just the right amount of wrapping for me.

  5. Sharky Says:

    Yeah true.

    Hey, dczraptor, thanks for pointing that out to me. I can’t believe I hadn’t noticed the context sensitive properties for that plugin. Must be code blindness. ;)

    I think I must have an older version, because I don’t see an option to edit the code in mine.

    Being able to make it fixed width makes #2 quite compelling again. I’ll give it some more thought.

    #3’s HTML is a bit more readable too (not that that really matters).

  6. dczraptor Says:

    Yea, I probably have a newer version, cause I only went to test it after I read your post. You can even specify the width constraints using the options for #2, so you can get the code to wrap precisely the correct amount. For #1 and #3, the code doesn’t wrap around at all for me (IE7), so longer lines might have code that gets cut off.

  7. Ultrahead Says:

    The same here: IE 7. #2 wraps the code right (as dczraptor says the latest version lets you specify certain things in a custom editor).

    For #1 and #3, you have to manually change the tags, for instance, to and or including everything in .

  8. Ultrahead Says:

    ooops, my tags dissapeared: here we go again!

    … change the tags, for instance, “pre” to “p” and or including everything in a panel (”div”).

Leave a Reply