Image.Save Method (String)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Saves this Image to the specified file or stream.

Namespace:   System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)

Public Sub Save (
	filename As String


Type: System.String

A string that contains the name of the file to which to save this Image.

Exception Condition

filename is null.


The image was saved with the wrong image format.


The image was saved to the same file it was created from.

If no encoder exists for the file format of the image, the Portable Network Graphics (PNG) encoder is used. When you use the Save method to save a graphic image as a Windows Metafile Format (WMF) or Enhanced Metafile Format (EMF) file, the resulting file is saved as a Portable Network Graphics (PNG) file. This behavior occurs because the GDI+ component of the .NET Framework does not have an encoder that you can use to save files as .wmf or .emf files.

Saving the image to the same file it was constructed from is not allowed and throws an exception.

The following code example demonstrates how to call the Save method. This example is designed to be used with Windows Forms. Create a form that contains a button named Button5. Paste the code into the form, and associate the method with the button's Click event.

Private Sub Button5_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button5.Click
        If (image1 IsNot Nothing) Then
            Button5.Text = "Saved file."
        End If
    Catch ex As Exception
        MessageBox.Show("There was a problem saving the file." _
        & "Check the file permissions.")
    End Try

End Sub

.NET Framework
Available since 1.1
Return to top