Quality of a saved JPG in C#
I made a small C# app to create an image in .jpg format.
pictureBox.Image.Save(name,ImageFormat.Jpeg);
The image is succesfully created. I input an original pic, do some stuff with it and save it. The quality of this new pic however, is lower than that of the original.
Is there any way to set the desired quality?
The following code example demonstrates how to create a EncoderParameter using the EncoderParameter constructor. To run this example, paste the code and call the VaryQualityLevel
method.
This example requires an image file named TestPhoto.jpg located at c:.
private void VaryQualityLevel()
{
// Get a bitmap.
Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
// Create an Encoder object based on the GUID
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder =
System.Drawing.Imaging.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.
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder,
50L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jgpEncoder,
myEncoderParameters);
myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityHundred.jpg", jgpEncoder,
myEncoderParameters);
// Save the bitmap as a JPG file with zero quality level compression.
myEncoderParameter = new EncoderParameter(myEncoder, 0L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityZero.jpg", jgpEncoder,
myEncoderParameters);
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
Ref: http://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoderparameter.aspx
Here's an even more compact chunk of code for saving as JPEG with a specific quality:
var encoder = ImageCodecInfo.GetImageEncoders().First(c => c.FormatID == ImageFormat.Jpeg.Guid);
var encParams = new EncoderParameters() { Param = new[] { new EncoderParameter(Encoder.Quality, 90L) } };
image.Save(path, encoder, encParams);
Or, if 120 character wide lines are too long for you:
var encoder = ImageCodecInfo.GetImageEncoders()
.First(c => c.FormatID == ImageFormat.Jpeg.Guid);
var encParams = new EncoderParameters(1);
encParams.Param[0] = new EncoderParameter(Encoder.Quality, 90L);
image.Save(path, encoder, encParams);
Make sure the quality is a long
or you will get an ArgumentException
!
This is an old thread, but I have rewritten the Microsoft (as per Dustin Getz answer) to be a little more useful - shrinking GetEncoderInfo and making an extension on Image. Anyway nothing really new, but may be of use:
/// <summary>
/// Retrieves the Encoder Information for a given MimeType
/// </summary>
/// <param name="mimeType">String: Mimetype</param>
/// <returns>ImageCodecInfo: Mime info or null if not found</returns>
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
var encoders = ImageCodecInfo.GetImageEncoders();
return encoders.FirstOrDefault( t => t.MimeType == mimeType );
}
/// <summary>
/// Save an Image as a JPeg with a given compression
/// Note: Filename suffix will not affect mime type which will be Jpeg.
/// </summary>
/// <param name="image">Image: Image to save</param>
/// <param name="fileName">String: File name to save the image as. Note: suffix will not affect mime type which will be Jpeg.</param>
/// <param name="compression">Long: Value between 0 and 100.</param>
private static void SaveJpegWithCompressionSetting(Image image, string fileName, long compression)
{
var eps = new EncoderParameters(1);
eps.Param[0] = new EncoderParameter(Encoder.Quality, compression);
var ici = GetEncoderInfo("image/jpeg");
image.Save(fileName, ici, eps);
}
/// <summary>
/// Save an Image as a JPeg with a given compression
/// Note: Filename suffix will not affect mime type which will be Jpeg.
/// </summary>
/// <param name="image">Image: This image</param>
/// <param name="fileName">String: File name to save the image as. Note: suffix will not affect mime type which will be Jpeg.</param>
/// <param name="compression">Long: Value between 0 and 100.</param>
public static void SaveJpegWithCompression(this Image image, string fileName, long compression)
{
SaveJpegWithCompressionSetting( image, fileName, compression );
}