Image.Save Method

Definition

Saves this image to the specified stream in the specified format.

Overloads

Save(String, ImageCodecInfo, EncoderParameters)

Saves this Image to the specified file, with the specified encoder and image-encoder parameters.

Save(Stream, ImageCodecInfo, EncoderParameters)

Saves this image to the specified stream, with the specified encoder and image encoder parameters.

Save(String, ImageFormat)

Saves this Image to the specified file in the specified format.

Save(Stream, ImageFormat)

Saves this image to the specified stream in the specified format.

Save(String)

Saves this Image to the specified file or stream.

Save(String, ImageCodecInfo, EncoderParameters)

Source:
Image.cs
Source:
Image.cs
Source:
Image.cs
Source:
Image.cs
Source:
Image.cs
Source:
Image.cs

Saves this Image to the specified file, with the specified encoder and image-encoder parameters.

C#
public void Save(string filename, System.Drawing.Imaging.ImageCodecInfo encoder, System.Drawing.Imaging.EncoderParameters? encoderParams);
C#
public void Save(string filename, System.Drawing.Imaging.ImageCodecInfo encoder, System.Drawing.Imaging.EncoderParameters encoderParams);

Parameters

filename
String

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

encoder
ImageCodecInfo

The ImageCodecInfo for this Image.

encoderParams
EncoderParameters

An EncoderParameters to use for this Image.

Exceptions

filename or encoder is null.

The image was saved with the wrong image format.

-or-

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

Examples

The following example creates a Bitmap object from a BMP file. The code saves the bitmap to three JPEG files, each with a different quality level.

C#
using System;
using System.Drawing;
using System.Drawing.Imaging;
class Example_SetJPEGQuality
{
    public static void Main()
    {
        Bitmap myBitmap;
        ImageCodecInfo myImageCodecInfo;
        Encoder myEncoder;
        EncoderParameter myEncoderParameter;
        EncoderParameters myEncoderParameters;
                     
        // Create a Bitmap object based on a BMP file.
        myBitmap = new Bitmap("Shapes.bmp");
                     
        // Get an ImageCodecInfo object that represents the JPEG codec.
        myImageCodecInfo = GetEncoderInfo("image/jpeg");
                     
        // Create an Encoder object based on the GUID
                     
        // for the Quality parameter category.
        myEncoder = Encoder.Quality;
                     
        // Create an EncoderParameters object.
                     
        // An EncoderParameters object has an array of EncoderParameter
                     
        // objects. In this case, there is only one
                     
        // EncoderParameter object in the array.
        myEncoderParameters = new EncoderParameters(1);
                     
        // Save the bitmap as a JPEG file with quality level 25.
        myEncoderParameter = new EncoderParameter(myEncoder, 25L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        myBitmap.Save("Shapes025.jpg", myImageCodecInfo, myEncoderParameters);
                     
        // Save the bitmap as a JPEG file with quality level 50.
        myEncoderParameter = new EncoderParameter(myEncoder, 50L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        myBitmap.Save("Shapes050.jpg", myImageCodecInfo, myEncoderParameters);
                     
        // Save the bitmap as a JPEG file with quality level 75.
        myEncoderParameter = new EncoderParameter(myEncoder, 75L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        myBitmap.Save("Shapes075.jpg", myImageCodecInfo, myEncoderParameters);
    }
    private static ImageCodecInfo GetEncoderInfo(String mimeType)
    {
        int j;
        ImageCodecInfo[] encoders;
        encoders = ImageCodecInfo.GetImageEncoders();
        for(j = 0; j < encoders.Length; ++j)
        {
            if(encoders[j].MimeType == mimeType)
                return encoders[j];
        }
        return null;
    }
}

Remarks

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

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Save(Stream, ImageCodecInfo, EncoderParameters)

Source:
Image.cs
Source:
Image.cs
Source:
Image.cs
Source:
Image.cs
Source:
Image.cs
Source:
Image.cs

Saves this image to the specified stream, with the specified encoder and image encoder parameters.

C#
public void Save(System.IO.Stream stream, System.Drawing.Imaging.ImageCodecInfo encoder, System.Drawing.Imaging.EncoderParameters? encoderParams);
C#
public void Save(System.IO.Stream stream, System.Drawing.Imaging.ImageCodecInfo encoder, System.Drawing.Imaging.EncoderParameters encoderParams);

Parameters

stream
Stream

The Stream where the image will be saved.

encoder
ImageCodecInfo

The ImageCodecInfo for this Image.

encoderParams
EncoderParameters

An EncoderParameters that specifies parameters used by the image encoder.

Exceptions

stream is null.

The image was saved with the wrong image format.

Remarks

Do not save an image to the same stream that was used to construct the image. Doing so might damage the stream.

The image must be saved to the stream at an offset of zero. If any additional data has been written to the stream before saving the image, the image data in the stream will be corrupted.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Save(String, ImageFormat)

Source:
Image.cs
Source:
Image.cs
Source:
Image.cs
Source:
Image.cs
Source:
Image.cs
Source:
Image.cs

Saves this Image to the specified file in the specified format.

C#
public void Save(string filename, System.Drawing.Imaging.ImageFormat format);

Parameters

filename
String

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

format
ImageFormat

The ImageFormat for this Image.

Exceptions

filename or format is null.

The image was saved with the wrong image format.

-or-

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

Examples

The following code example demonstrates how to construct a bitmap from a type, and how to use the Save method. To run this example, paste the code into a Windows Form. Handle the form's Paint event, and call the ConstructFromResourceSaveAsGif method, passing e as PaintEventArgs

C#
private void ConstructFromResourceSaveAsGif(PaintEventArgs e)
{

    // Construct a bitmap from the button image resource.
    Bitmap bmp1 = new Bitmap(typeof(Button), "Button.bmp");

    // Save the image as a GIF.
    bmp1.Save("c:\\button.gif", System.Drawing.Imaging.ImageFormat.Gif);

    // Construct a new image from the GIF file.
    Bitmap bmp2 = new Bitmap("c:\\button.gif");

    // Draw the two images.
    e.Graphics.DrawImage(bmp1, new Point(10, 10));
    e.Graphics.DrawImage(bmp2, new Point(10, 40));

    // Dispose of the image files.
    bmp1.Dispose();
    bmp2.Dispose();
}

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Save(Stream, ImageFormat)

Source:
Image.cs
Source:
Image.cs
Source:
Image.cs
Source:
Image.cs
Source:
Image.cs
Source:
Image.cs

Saves this image to the specified stream in the specified format.

C#
public void Save(System.IO.Stream stream, System.Drawing.Imaging.ImageFormat format);

Parameters

stream
Stream

The Stream where the image will be saved.

format
ImageFormat

An ImageFormat that specifies the format of the saved image.

Exceptions

stream or format is null.

The image was saved with the wrong image format.

Remarks

You should avoid saving an image to the same stream that was used to construct it. Doing so might damage the stream.

The image must be saved to the stream at an offset of zero. If any additional data has been written to the stream before saving the image, the image data in the stream will be corrupted.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Save(String)

Source:
Image.cs
Source:
Image.cs
Source:
Image.cs
Source:
Image.cs
Source:
Image.cs
Source:
Image.cs

Saves this Image to the specified file or stream.

C#
public void Save(string filename);

Parameters

filename
String

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

Exceptions

filename is null.

The image was saved with the wrong image format.

-or-

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

Examples

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.

C#
private void Button5_Click(System.Object sender, System.EventArgs e)
{
    try
    {
        if (image1 != null)
        {
            image1.Save("c:\\myBitmap.bmp");
            Button5.Text = "Saved file.";
        }
    }
    catch(Exception)
    {
        MessageBox.Show("There was a problem saving the file." +
            "Check the file permissions.");
    }
}

Remarks

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.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10