Save a bitmap showing user drawn line segments in C#

[save a bitmap]

The example Draw, move, and delete line segments in C# lets the user draw line segments, but it doesn’t include a way to save a bitmap showing the result. This example provides the same features as the previous one, plus it allows you to save a bitmap showing the lines that you drew. When you select the File menu’s Save As command, the program displays a SaveFileDialog. If you select a png, bmp, or jpg file, the program draws the line segments onto a bitmap and saves it in the file that you selected.

When you select the Save As menu item, the following event handler executes.

// Save the drawing.
private void mnuFileSave_Click(object sender, EventArgs e)
{
    if (sfdPicture.ShowDialog() != DialogResult.OK)
        return;

    // Make a bitmap that fits the PictureBox.
    Bitmap bm = new Bitmap(
        picCanvas.ClientSize.Width,
        picCanvas.ClientSize.Height);

    // Draw.
    using (Graphics gr = Graphics.FromImage(bm))
    {
        gr.SmoothingMode = SmoothingMode.AntiAlias;
        for (int i = 0; i < Pt1.Count; i++)
            gr.DrawLine(Pens.Blue, Pt1[i], Pt2[i]);
    }

    // Save the result.
    SaveImage(bm, sfdPicture.FileName);
}

This code first displays the SaveFileDialog. If you close the dialog without selecting a file, the event handler exits.

If you do select a file, the code creates a Bitmap that has the same size as the client area of the program’s PictureBox.
If makes a Graphics object associated with the bitmap and sets its SmoothingMode property to draw smooth lines. The code then loops through the lines that you drew and draws them on the Graphics object.

The code finishes by calling the SaveImage method to save the bitmap in the format that is appropriate for the file’s name. For example, if the file’s name ends with .png, then the method saves the bitmap with the PNG file format. See the post Save images with an appropriate format depending on the file name’s extension in C# for information about the SaveImage method.

Note that the saved bitmap has the same size as the client area of the program’s PictureBox. That means any parts of lines that lie outside of the PictureBox control’s client area will not fit on the bitmap. If you need to save segments that lie outside of that area, you can modify the code to use a larger bitmap. For example, you could loop through all of the line segments and size the bitmap so it is big enough to hold all of the segments’ end points. You could also translate the drawing if some of the end points have negative X or Y coordinates.

Download the example to see additional details.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in drawing, files, graphics and tagged , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.