Click here to Skip to main content
15,881,173 members
Articles / DevOps
Tip/Trick

Save WinForm Snapshot Images

Rate me:
Please Sign up or sign in to vote.
3.60/5 (5 votes)
2 Aug 2018CPOL 15.6K   8   2
Simple C# methods for saving snapshot images of a WinForm to the desktop

Introduction

As a tip for users/developers of visualization desktop applications, I’d like to call attention to two methods which I have found very useful. One of these simply saves the entire Form as a .png file on the desktop. Getting that to work right seemed tricky to me but very worthwhile. The other method just checks to see if a filename already exists and, if so, adds an incremented copy number to the pathname. These two methods make saving snapshots of a form simple and quick.

Using the Code

Typically, I place a "Save Image" button near a corner of my Form using the designer and set Save_Image_Button_Click() as the Click event for that button.

C#
/// <summary>
/// Save the Form Image to the Desktop
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Save_Image_Button_Click(object sender, EventArgs e)
{
    string MyDesktopPath = Environment.GetFolderPath(
        System.Environment.SpecialFolder.DesktopDirectory) + "\\";

    using (Bitmap bmpScreenshot = new Bitmap(Bounds.Width, Bounds.Height, PixelFormat.Format32bppArgb))
    {
        Rectangle rect = new Rectangle(0, 0, this.Bounds.Width, this.Bounds.Height);
        this.DrawToBitmap(bmpScreenshot, rect);

        // Build the filename
        string myFilepath = SaveFileNameCheck(MyDesktopPath, "MyFormName.png");
        if (myFilepath == "")
        {
            Console.WriteLine("Form image save FAILED!");
            return;
        }
        bmpScreenshot.Save(myFilepath, ImageFormat.Png);
        Console.WriteLine("Form image saved: {0}", myFilepath);
    }
}

/// <summary>
/// If filename exists, add or increment a # before any extension and check again
/// </summary>
/// <param name="path">Directory path to file location</param>
/// <param name="filename">Filename (to be incremented if existent)</param>
/// <returns>Complete pathname for file, possibly incremented, with original extension, if any</returns>
string SaveFileNameCheck(string path, string filename)
{
    if (!Directory.Exists(path)) return "";
    string filenamewoext = Path.GetFileNameWithoutExtension(filename);
    string extension = Path.GetExtension(filename);
    int count = 1;
    while (File.Exists(path + filename))
    {
        filename = filenamewoext + "(" + count.ToString() + ")" + extension;
        count++;
    }
    return path + filename;
}

History

  • 2nd August, 2018: Version 1.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Academic Software, Inc.
United States United States
My public profile: http://www.linkedin.com/in/warrenlacefield
My research profile: https://www.researchgate.net/profile/Warren-Lacefield
My company homepage: http://www.acsw.com

Comments and Discussions

 
QuestionNamespace Pin
RobScripta6-Aug-18 11:39
professionalRobScripta6-Aug-18 11:39 
AnswerRe: Namespace Pin
asiwel6-Aug-18 17:23
professionalasiwel6-Aug-18 17:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.