Image.Save Method (String, ImageCodecInfo, EncoderParameters)
.NET Framework 4
Saves this Image to the specified file, with the specified encoder and image-encoder parameters.
Assembly: System.Drawing (in System.Drawing.dll)
public void Save( string filename, ImageCodecInfo encoder, EncoderParameters encoderParams )
Parameters
- filename
- Type: System.String
A string that contains the name of the file to which to save this Image.
- encoder
- Type: System.Drawing.Imaging.ImageCodecInfo
The ImageCodecInfo for this Image.
- encoderParams
- Type: System.Drawing.Imaging.EncoderParameters
An EncoderParameters to use for this Image.
| Exception | Condition |
|---|---|
| ArgumentNullException |
filename or encoder is null. |
| ExternalException |
The image was saved with the wrong image format. -or- The image was saved to the same file it was created from. |
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.
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; } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Extension Methods to aide with encoders
This may help to get:
- mime type from Image
- get an encoder for the mime type or a default encoder
public static class ImageExtensions
{
public static string GetMimeType(this System.Drawing.Image image)
{
string sReturn = string.Empty;
if (image.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Bmp.Guid)
sReturn = "image/bmp";
else if (image.RawFormat.Guid.Equals(System.Drawing.Imaging.ImageFormat.Emf.Guid))
sReturn = "image/emf";
else if (image.RawFormat.Guid.Equals(System.Drawing.Imaging.ImageFormat.Exif.Guid))
sReturn = "image/exif";
else if (image.RawFormat.Guid.Equals(System.Drawing.Imaging.ImageFormat.Gif.Guid))
sReturn = "image/gif";
else if (image.RawFormat.Guid.Equals(System.Drawing.Imaging.ImageFormat.Icon.Guid))
sReturn = "image/icon";
else if (image.RawFormat.Guid.Equals(System.Drawing.Imaging.ImageFormat.Jpeg.Guid))
sReturn = "image/jpeg";
else if (image.RawFormat.Guid.Equals(System.Drawing.Imaging.ImageFormat.MemoryBmp.Guid))
sReturn = "image/membmp";
else if (image.RawFormat.Guid.Equals(System.Drawing.Imaging.ImageFormat.Png.Guid))
sReturn = "image/png";
else if (image.RawFormat.Guid.Equals(System.Drawing.Imaging.ImageFormat.Tiff.Guid))
sReturn = "image/tiff";
else if (image.RawFormat.Guid.Equals(System.Drawing.Imaging.ImageFormat.Wmf.Guid))
sReturn = "image/wmf";
else
sReturn = "image/jpeg";
return sReturn;
}
public static ImageCodecInfo GetEncoderInfoOrDefault(this string mimeType)
{
var encoders = ImageCodecInfo.GetImageEncoders();
var existingEnc = encoders.FirstOrDefault(enc => enc.MimeType.Equals(mimeType));
// should the specific encoder not be found, then return the JPG encoder by default.
if (existingEnc == null)
{
existingEnc = encoders.SingleOrDefault(enc => enc.MimeType.Equals("image/jpeg"));
}
// should the default encoder not exist, then return any encoder or null if none is present.
if (existingEnc == null)
{
existingEnc = (encoders.Length > 0) ? encoders[0] : null;
}
return existingEnc;
}
public static ImageCodecInfo GetEncoderInfoOrDefault(this System.Drawing.Image image)
{
return GetEncoderInfoOrDefault(image.GetMimeType());
}
}