How to: Set JPEG Compression Level
You may want to modify the parameters of an image when you save the image to disk to minimize the file size or improve its quality. You can adjust the quality of a JPEG image by modifying its compression level. To specify the compression level when you save a JPEG image, you must create an EncoderParameters object and pass it to the Save method of the Image class. Initialize the EncoderParameters object so that it has an array that consists of one EncoderParameter. When you create the EncoderParameter, specify the Quality encoder, and the desired compression level.
Example
The following example code creates an EncoderParameter object and saves three JPEG images. Each JPEG image is saved with a different quality level, by modifying the long value passed to the EncoderParameter constructor. A quality level of 0 corresponds to the greatest compression, and a quality level of 100 corresponds to the least compression.
Private Sub VaryQualityLevel()
' Get a bitmap.
Dim bmp1 As New Bitmap("c:\TestPhoto.jpg")
Dim jgpEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
' Create an Encoder object based on the GUID
' for the Quality parameter category.
Dim myEncoder As System.Drawing.Imaging.Encoder = 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.
Dim myEncoderParameters As New EncoderParameters(1)
Dim myEncoderParameter As New EncoderParameter(myEncoder, 50&)
myEncoderParameters.Param(0) = myEncoderParameter
bmp1.Save("c:\TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters)
myEncoderParameter = New EncoderParameter(myEncoder, 100&)
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, 0&)
myEncoderParameters.Param(0) = myEncoderParameter
bmp1.Save("c:\TestPhotoQualityZero.jpg", jgpEncoder, myEncoderParameters)
End Sub 'VaryQualityLevel
...
Private Function GetEncoder(ByVal format As ImageFormat) As ImageCodecInfo
Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageDecoders()
Dim codec As ImageCodecInfo
For Each codec In codecs
If codec.FormatID = format.Guid Then
Return codec
End If
Next codec
Return Nothing
End Function
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;
}
Compiling the Code
This example requires:
A Windows Forms application.
A PaintEventArgs, which is a parameter of PaintEventHandler.
An image file that is named TestPhoto.jpg and located at c:\.
See Also
Tasks
How to: Determine the Parameters Supported by an Encoder