How to make a Print Screen on Microsoft Source using WPF

Making a Printscreen with WPF on Microsoft Surface - Programming with C# and using WCF / WPF and Silverlight

After we took a look inside of Microsoft Surface, let’s see how to take a later look on the surface, by creating printscreens of the application.

In some WPF applications you’ll need to take a quick screenshot of the users’ screen, allowing him (or them, in case of Multi User Surface) for later reviewing. Now, on Surface there can be only one full screen and active application. So it use the entire screen of the device.

Here are the steps in order to accomplish that:

First, we need to add to Visual Studio project the references to the libraries that we’ll use:

using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

Then, we will add create the method that will do the actual saving to a specific file, with the format PNG:

public void MakeScreenshot(String fileName)
{
    Bitmap bmpScreenshot;
    Graphics gfxScreenshot;

    //first, we create&set a bitmap object to the size of the screen
    bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);

    //Using the bitmap, we create a graphics object
    gfxScreenshot = Graphics.FromImage(bmpScreenshot);

    //Copy the rectangle (the screen in our case) - from the upper left corner to the right bottom corner
    gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

    // Do the saving, outputing a file by supplying the name and format it as PNG
    bmpScreenshot.Save(fileName, ImageFormat.Png);
}

That’s all, folks – Use and enjoy!