Saving an Image

Image Editing Tutorials: For C# and VB NET Students

 

You'll be glad to know that saving an image is quite easy!

Add a SaveFileDialog control to your form from the C# toolbox. Change the Name property to saveFD. Add a new button to your form and change the Text property to Save. Double click your button to get at the code stub. Now add the following in C#:

saveFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

saveFD.FileName = "Save Image";
saveFD.Filter = "JPEG|*.jpeg";

And this in VB:

saveFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)

saveFD.FileName = "Save Image"
saveFD.Filter = "JPEG|*.jpeg"

We've already covered Save dialogue boxes in a previous section, so we won’t explain what it all means here. But we're just setting the save directory to default to the My Documents folder in Windows (or the MyPictures folder, whichever you prefer). The filter has been set to JPEG images. You'll see why in a moment.

Now add the following if statement in C#:

if (saveFD.ShowDialog() != DialogResult.Cancel)
{

}

And this one in VB Net:

If saveFD.ShowDialog() <> DialogResult.Cancel Then

End If

As the first line of your if statement add these two lines:

C#

savePath = saveFD.FileName;

Bitmap bmp = new Bitmap(pictureBox1.Image);

VB

Dim savePath As String = saveFD.FileName

Dim bmp As Bitmap = New Bitmap(PictureBox1.Image)

The dialogue boxes, remember, just return a file path – they don't actually save anything. We're putting the FileName into the string variable we've called savePath. The next line just gets the picture from the PictureBox, as before.

To do the actual saving, you can use the Save method of Bitmap objects.

The Save method has many alternatives. But one of them uses two arguments: a file path and an image format. Add this line to your if statement in C#:

bmp.Save(savePath, ImageFormat.Jpeg);

And this one in VB Net:

bmp.Save(savePath, Imaging.ImageFormat.Jpeg)

(If you didn’t add using System.Drawing.Imaging statement at the top in C# then add System.Drawing.Imaging.ImageFormat.Jpeg between the round brackets of Save.)

In between the round brackets we first have our file path. After a comma, we have this:

ImageFormat.Jpeg

ImageFormat is a class in the System.Drawing.Imaging namespace. When you type the dot after ImageFormat you'll see a list appear. This one:

Context menu showing an Image Format option selected

This is a list of image formats that the Bitmap can save to. We've gone for the JPEG image format.

As the final line of the Save code you can add a message box, if you like. The whole of the code would then look like this in C#:

C# code to save a bitmap as a JPEG image

And this in VB Net:

VB Net code to save a bitmap as a JPEG image

Run your program and test it out. Load an image into your PictureBox. Click one of your buttons, such as Filter or Invert. Then click your Save button. You should find that you'll have a new image in your My Documents or My Pictures folder.

 

And that's it for image manipulation. We hope it has whetted your appetite to explore more!

Back to the C# NET Contents Page

Back to the VB NET Contents Page