Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c#: reduced image quality when saving JPEG at 100% quality

Tags:

image

save

jpeg

I'm just loading JPEG image and saving it without any manipulation with it. But image quality noticeably reducing.

Here is the code:

Bitmap imgOutput = new Bitmap(@"D:\image.jpg");
Graphics outputGraphics = Graphics.FromImage(imgOutput);      

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);

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
    if (codec.MimeType == "image/jpeg")
        ici = codec;
}

imgOutput.Save(@"D:\result.jpg", ici, myEncoderParameters);

And what I get: https://i.sstatic.net/4KLHV.jpg zoom: https://i.sstatic.net/zm8t9.jpg

Is there another image quality settings?

like image 268
user1032559 Avatar asked Dec 28 '25 09:12

user1032559


1 Answers

JPEG, being a lossy format introduces loss on every save. It is also particularly bad at compressing sharp edges. According to this MSDN article, you're setting the image quality correctly. You can play with the other settings if you want to try and optimize the image quality, as I don't know how you came up with the ScanMethod and RenderMethod. However, the best way in my opinion is to use a lossless format (PNG, TIFF, etc).

Update

It appears that this is due to the bad GDI+ JPEG encoder. More on it on the MSDN forums. The conclusion is - use a third party imaging library, there are plenty free ones out there.

like image 198
Ivailo Karamanolev Avatar answered Dec 30 '25 23:12

Ivailo Karamanolev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!