Have a Snippet?

Keep, track and share your code snippets with your friends



C#: Create a screenshot Share on Vkontakte

If you want to create a screenshot out of your application, this is the perfect snippet suitable to your needs.

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

/// 
/// Saves an image of the screen to the specified path.
/// 
/// 
/// Path, where output file will be saved at.
/// Path of the successfully saved image or errormessage

public string ScreenToPicture(string Location)
{
    try
    {
        Size currentScreenSize = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        Bitmap ScreenToBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        System.Drawing.Graphics gGraphics = System.Drawing.Graphics.FromImage(ScreenToBitmap);

        gGraphics.CopyFromScreen(new Point(0, 0), new Point(0, 0), currentScreenSize);
        ScreenToBitmap.Save(Location);

        return Location;

    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}


Tag: C#, .NET, screenshot, Drawing

0 Comments