none
Png image conver to JPEG and optimize the image quality RRS feed

  • Question

  • I try to transfer PNG to JPEG and set it background white

    Here is code

    using System.Drawing;
    
    System.Drawing.Image img = System.Drawing.Image.FromFile(@"D:\image\test.png");
                // Assumes myImage is the PNG you are converting
                using (var b = new Bitmap(img.Width, img.Height))
                {
                    b.SetResolution(img.HorizontalResolution, img.VerticalResolution);
    
                    using (var g = Graphics.FromImage(b))
                    {
                        g.Clear(System.Drawing.Color.White);
                        g.DrawImageUnscaled(img, 0, 0);
                    }
    
                    // Now save b as a JPEG like you normally would
                    b.Save(@"D:\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                }

    But it will blur like below image

    In image red and blue region will look blur.

    Is there any method to solve this?

    And image still jpeg.

    I put image below

    Thanks




    • Edited by CY_Chen Tuesday, September 17, 2019 5:00 AM
    Monday, September 16, 2019 8:54 AM

Answers

  • Hi   CY_Chen,

    JPEG, being a lossy format introduces loss on every save. It is also particularly bad at compressing sharp edges. 

    You can setting the image 100% quality. You can play with the other settings if you want to try and optimize the image quality.

              System.Drawing.Image img = System.Drawing.Image.FromFile(@"D:\testimage.PNG");
               
                using (var b = new Bitmap(img.Width, img.Height))
                {
                    b.SetResolution(img.HorizontalResolution, img.VerticalResolution);
    
                    using (var g = Graphics.FromImage(b))
                    {
                        g.Clear(System.Drawing.Color.White);
                        g.DrawImageUnscaled(img, 0, 0);
                    }
                    ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
                    
                    System.Drawing.Imaging.Encoder myEncoder =
                        System.Drawing.Imaging.Encoder.Quality;
                    // EncoderParameter object in the array.
                    EncoderParameters myEncoderParameters = new EncoderParameters(3);
                    myEncoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
                    myEncoderParameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.ScanMethod, (int)EncoderValue.ScanMethodInterlaced);
                    myEncoderParameters.Param[2] = new EncoderParameter(System.Drawing.Imaging.Encoder.RenderMethod, (int)EncoderValue.RenderProgressive);
    
                    b.Save(@"D:\test.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;
            }
    

    But, the 100% quality still does not mean lossless compression when using JPEG. Loading the image, doing something to it, and then saving it again will ultimately result in image degradation. If you need to alter and save an image without losing any of the data you need to use a lossless format such as TIFF, PNG or BMP. or PNG. Or, you can use some third-party libraries.

    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CY_Chen Tuesday, September 17, 2019 5:27 AM
    Tuesday, September 17, 2019 4:34 AM
    Moderator