Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 589
KdgDev Avatar asked Sep 27 '09 23:09

KdgDev


People also ask

What is the quality of a JPEG?

The amount of JPEG compression is typically measured as a percentage of the quality level. An image at 100% quality has (almost) no loss, and 1% quality is a very low quality image. In general, quality levels of 90% or higher are considered "high quality", 80%-90% is "medium quality", and 70%-80% is low quality.

Why is JPG low quality?

JPGs are destructive to image quality because this file type doesn't preserve every pixel of color within an image, and the more you save it the lower the quality. JPG is also particularly hard on text—especially small text.

What is ImageCodecInfo?

The ImageCodecInfo class provides the necessary storage members and methods to retrieve all pertinent information about the installed image encoders and decoders (called codecs). Not inheritable.


2 Answers

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

like image 90
4 revs, 4 users 72% Avatar answered Sep 19 '22 00:09

4 revs, 4 users 72%


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!

like image 39
Roman Starkov Avatar answered Sep 18 '22 00:09

Roman Starkov