--- title: "How to: Set JPEG Compression Level" description: Learn how to adjust the quality of a JPEG image by modifying its compression level on Windows Forms. ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "images [Windows Forms], changing encoder parameters" - "JPEG images [Windows Forms], setting quality level" ms.assetid: 4b9a74e3-9504-43c1-9f28-ace651d0772e --- # 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 object and pass it to the method of the class. Initialize the object so that it has an array that consists of one . When you create the , specify the encoder, and the desired compression level. ## Example The following example code creates an object and saves three JPEG images. Each JPEG image is saved with a different quality level, by modifying the `long` value passed to the constructor. A quality level of 0 corresponds to the greatest compression, and a quality level of 100 corresponds to the least compression. ```csharp private void VaryQualityLevel() { // Get a bitmap. The using statement ensures objects // are automatically disposed from memory after use. using (Bitmap bmp1 = new Bitmap(@"C:\TestPhoto.jpg")) { ImageCodecInfo jpgEncoder = 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", jpgEncoder, myEncoderParameters); myEncoderParameter = new EncoderParameter(myEncoder, 100L); myEncoderParameters.Param[0] = myEncoderParameter; bmp1.Save(@"C:\TestPhotoQualityHundred.jpg", jpgEncoder, 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", jpgEncoder, myEncoderParameters); } } ``` ```vb Private Sub VaryQualityLevel() ' Get a bitmap. The Using statement ensures objects ' are automatically disposed from memory after use. Using bmp1 As New Bitmap("C:\TestPhoto.jpg") Dim jpgEncoder 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, 50L) myEncoderParameters.Param(0) = myEncoderParameter bmp1.Save("c:\TestPhotoQualityFifty.jpg", jpgEncoder, myEncoderParameters) myEncoderParameter = New EncoderParameter(myEncoder, 100L) myEncoderParameters.Param(0) = myEncoderParameter bmp1.Save("C:\TestPhotoQualityHundred.jpg", jpgEncoder, 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", jpgEncoder, myEncoderParameters) End Using End Sub ``` ```csharp private ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); foreach (ImageCodecInfo codec in codecs) { if (codec.FormatID == format.Guid) { return codec; } } return null; } ``` ```vb Private Function GetEncoder(ByVal format As ImageFormat) As ImageCodecInfo Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders() 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 ``` ## Compiling the Code This example requires: - A Windows Forms application. - A , which is a parameter of . - An image file that is named `TestPhoto.jpg` and located at **c:\\**. ## See also - [How to: Determine the Parameters Supported by an Encoder](how-to-determine-the-parameters-supported-by-an-encoder.md) - [Types of Bitmaps](types-of-bitmaps.md) - [Using Image Encoders and Decoders in Managed GDI+](using-image-encoders-and-decoders-in-managed-gdi.md)