The instance code in C to save the image at the specified quality

  • 2020-05-27 07:03:21
  • OfStack

Production of jpg images in the program is not as good as the original image, because Microsoft's Image.Save method saves the image compression quality of 75, so the saved image quality is low. In order to improve the quality of the generated image, we need to set the quality parameters of EncoderParameters class and the image saving format of ImageCodecInfo class.

System. Drawing. Imaging. Encoder class to develop need to present the way and various parameters, such as image quality parameters, scan method parameters, chromaticity chart parameters, the compression parameter, color depth, etc. To this, you should know that modify the steps and methods of image quality. The main is to System. Drawing. Imaging. Encoder Settings, and then through System. Drawing. Imaging. Encoder EncoderParameter derived class, pass EncoderParameter EncoderParameters array, is obtained 1 photo that is exactly what you want! Oh, said a lot. In fact, the code is very simple:

First: using System.Drawing.Imaging;


/// <summary>
        ///  Save the image to the specified compression quality and format (Microsoft's Image.Save Method to save to image compression quality as 75)
        /// </summary>
        /// <param name="sourceImage"> Want to save the picture of Image object </param>
        /// <param name="savePath"> The absolute path to save the image </param>
        /// <param name="imageQualityValue"> The compression quality of the image to save, the value of this parameter is 1 to 100 The larger the value, the better the preservation quality </param>
        /// <returns> Save successfully, return true ; Instead, return false</returns>
        public bool SaveImageForSpecifiedQuality(System.Drawing.Image sourceImage, string savePath, int imageQualityValue)
        {
            // The following code sets the compression quality when saving the image 
            EncoderParameters encoderParameters = new EncoderParameters();
            EncoderParameter encoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, imageQualityValue);
            encoderParameters.Param[0] = encoderParameter;
            try
            {
                ImageCodecInfo[] ImageCodecInfoArray = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo jpegImageCodecInfo = null;
                for (int i = 0; i < ImageCodecInfoArray.Length; i++)
                {
                    if (ImageCodecInfoArray[i].FormatDescription.Equals("JPEG"))
                    {
                        jpegImageCodecInfo = ImageCodecInfoArray[i];
                        break;
                    }
                }    
                sourceImage.Save(savePath, jpegImageCodecInfo, encoderParameters);               
                return true;
            }
            catch
            {
                return false;
            }
        }

Call method:


System.Drawing.Image image = System.Drawing.Image.FromFile("D:\\TestImage\\0.jpg");
SaveImageForSpecifiedQuality(image, "D:\\TestImage\\1.jpg", 100, ImageFormat.Jpeg);image.Dispose();


Related articles: