none
Turning off JPEG color downsampling for Bitmap.Save()

    Question

  • The following code saves a Bitmap to a JPEG file with a quality setting of 100 (the maximum):

                // Find a JPEG encoder
                ImageCodecInfo jpegEncoderInfo = null;
                ImageCodecInfo[] encoderInfoArray = ImageCodecInfo.GetImageEncoders();
                foreach (ImageCodecInfo imageCodecInfo in encoderInfoArray)
                {
                    if (0 == String.Compare("image/jpeg", imageCodecInfo.MimeType, true))
                    {
                        jpegEncoderInfo = imageCodecInfo;
                    }
                }

                // If a JPEG encoder was found, use it to save a Bitmap to a JPEG file,
                // explicitly specifying a "quality" parameter of 100 (the maximum).
                if (null != jpegEncoderInfo)
                {
                    EncoderParameters encoderParameters = new EncoderParameters(1);
                    encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
                    bitmap.Save("image.jpg", jpegEncoderInfo, encoderParameters);
                }

    Comparing the image saved in this manner with an image saved via any typical application (e.g., Photoshop, etc) at a quality setting of 100 easily shows that the .NET framework's built-in JPEG codec is not producing the same sharpness at the maximum quality setting as typical image editing applications.

    After some searching I learned that the .NET framework's built-in JPEG codec is doing color downsampling (something like "YUV (1x1, 2x1, 2x1)" instead of non-downsampled "YUV 1x1, 1x1, 1x1)").  Thus, even though the quality setting may be set to the maximum, the .NET framework built-in JPEG codec is doing color (UV) downsampling, making the resulting image look really bad in some areas.

    Some developers mentioned using 3rd party APIs to save out JPEG images to avoid the color downsampling. :-(

    However, I'm hoping that I can simply set another JPEG encoder parameter to disable the color downsampling.

    Does such an encoder parameter exist for the .NET framework built-in JPEG encoder? 

    Please let there be a way to disable the color downsampling!  It would be super-lame if there were no way to disable the color downsampling.  After all, who could possibly think that color downsampling would be desirable for the majority, let alone all, of the applications producing JPEGs?  However, given the really lame default quality of the JPEG codec when the quality setting is not explicitly specified does make me fear that the Microsoft developers who created the built-in JPEG encoder didn't carefully consider how users of the .NET framework would likely use the JPEG encoder.  (My guess is that the majority of .NET framework users would want a much higher default quality setting -- and without color downsampling.  Do an online search for ".net bitmap.save blurry jpeg", or similar search keywords, to get a sense of the "confusion" over why the JPEGs look so bad.)

    I'm grateful that the .NET framework has built-in support for reading JPEG files -- and I think that the benefit to society due to that built-in support is significant -- but it would be sad if saving JPEGs just couldn't be done at high quality because of some strange decision to do color downsampling regardless of the image quality setting.

    --- Colin

    Sunday, May 20, 2007 10:20 AM

Answers

  • The well known deficiencies in the GDI+ image encoders created a lively aftermarket for graphics processing tools.  Well known solutions are LeadTools (commercial) and ImageMagick (Freeware).  You can ask for bug fixes at Product Feedback but you'll have to join the crowd.  GDI+ was frozen a long time ago.
    Sunday, May 20, 2007 11:03 AM
    Moderator

All replies

  • The well known deficiencies in the GDI+ image encoders created a lively aftermarket for graphics processing tools.  Well known solutions are LeadTools (commercial) and ImageMagick (Freeware).  You can ask for bug fixes at Product Feedback but you'll have to join the crowd.  GDI+ was frozen a long time ago.
    Sunday, May 20, 2007 11:03 AM
    Moderator
  • Noooooooooooo!!!  :-)

    I wish Microsoft would stop creating "lively aftermarkets" for all of their various products.  :-)  LOL !!!

    Okay, I am going to indicate that your response "answers" my problem.

    If "GDI+ was frozen a long time ago" means that Microsoft has no plans to offer a way to save Bitmap as high-quality JPEG files within the C#/.NET paradigm, then I think that's lame.  It's especially lame because all Microsoft has to do is NOT downsample the UV colors!

    (Aside: When I learned that Vista, Windows Media Player 11, and the Zune *intentionally* downsamples the quality of video whose DRM status could not be established, I was stunned.  Microsoft is clearly committed to the ongoing creation of lively aftermarkets!)

    Anyhow, thank you for confirming my fears.  Now I can move on to the "acceptance" phase and eventually make a full recovery.

    By the way, nobugz -- you have helped an amazing number of people.  I mean, even if some percentage of your help takes the form of breaking bad news and enabling the poor soul to move on with their lives, wow, you are getting the job done!  If you're a bot or an A.I., or a pseudonym used by an entire group of technical support employees of Microsoft, I could comprehend your staggering number of posts...But if you're a human being, and you aren't taking performance-enhancing drugs, I salute you!  Yay!

    --- Colin

    Sunday, May 20, 2007 1:48 PM
  • Thanks for the compliment!  No, I'm just one guy with an unhealthy addiction only to grateful posters.  Beer and a smoke, that's about my sins.  I'm soon going to run out of beer money though.
    Sunday, May 20, 2007 2:50 PM
    Moderator