Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 2.0
System.Drawing
Image Class
Image Methods
Save Method
 Save Method (Stream, ImageCodecInfo...
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Image.Save Method (Stream, ImageCodecInfo, EncoderParameters)
Saves this image to the specified stream, with the specified encoder and image encoder parameters.

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

Visual Basic (Declaration)
Public Sub Save ( _
    stream As Stream, _
    encoder As ImageCodecInfo, _
    encoderParams As EncoderParameters _
)
Visual Basic (Usage)
Dim instance As Image
Dim stream As Stream
Dim encoder As ImageCodecInfo
Dim encoderParams As EncoderParameters

instance.Save(stream, encoder, encoderParams)
C#
public void Save (
    Stream stream,
    ImageCodecInfo encoder,
    EncoderParameters encoderParams
)
C++
public:
void Save (
    Stream^ stream, 
    ImageCodecInfo^ encoder, 
    EncoderParameters^ encoderParams
)
J#
public void Save (
    Stream stream, 
    ImageCodecInfo encoder, 
    EncoderParameters encoderParams
)
JScript
public function Save (
    stream : Stream, 
    encoder : ImageCodecInfo, 
    encoderParams : EncoderParameters
)
XAML
Not applicable.

Parameters

stream

The Stream where the image will be saved.

encoder

The ImageCodecInfo for this Image.

encoderParams

An EncoderParameters that specifies parameters used by the image encoder.

Exception typeCondition

ArgumentNullException

stream is a null reference (Nothing in Visual Basic).

ExternalException

The image was saved with the wrong image format.

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

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Get an ImageCodecInfo for a specific ImageFormat      David M. Kean - MSFT   |   Edit   |   Show History

Unfortunately, .NET does not provide an easy way to get your hands on an ImageCodecInfo for a specific image format.

Instead, you can use something like the following:

C#:

public static ImageCodecInfo FindEncoder(ImageFormat format)
    if (format == null)
        throw new ArgumentNullException("format");
 
    foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())
    {
        if (codec.FormatID.Equals(format.Guid))
        {
            return codec;
        }
    }
 
    return null;
}

 

Visual Basic:

Public Shared Function FindEncoder(ByVal format As ImageFormat) As ImageCodecInfo
    If (format Is Nothing) Then Throw New ArgumentNullException("format")
 
    For Each codec As ImageCodecInfo In ImageCodecInfo.GetImageEncoders()
        If codec.FormatID.Equals(format.Guid) Then
            Return codec
        End If
    Next
 
    Return Nothing
End Function

 

To use this method and retrieve the correct encoder, simply pass an ImageFormat for the format you want to save the image as, and then pass the encoder to the Image.Save overload.

C#:

ImageCodecInfo encoder = FindEncoder(ImageFormat.Jpeg);

 

Visual Basic:

Dim encoder as ImageCodecInfo = FindEncoder(ImageFormat.Jpeg);
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker