简体   繁体   English

JPEG 压缩产生更大的尺寸

[英]JPEG Compression produces bigger size

I want to compress image quality of a jpg.我想压缩 jpg 的图像质量。

I am using the sample code from the Microsoft documentation below:我正在使用以下 Microsoft 文档中的示例代码:
https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-set-jpeg-compression-level?redirectedfrom=MSDN https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-set-jpeg-compression-level?redirectedfrom=MSDN

Whatever image I choose for testing purposes, the new image size is becoming bigger than the original one when choosing the quality between 85L and 100L.无论我出于测试目的选择哪种图像,在选择 85L 和 100L 之间的质量时,新的图像尺寸都比原来的大。 Starting down from 85L, it is getting less.从85L开始,越来越少。

I don't understand, why the image size is getting bigger using that code for the range 85L-100L.我不明白,为什么使用 85L-100L 范围内的代码图像尺寸会变大。

    void SaveImage(Image image, string destPath, long quality)
    {
        if (quality < 0 || quality > 100)
            throw new ArgumentException("quality has to be between 0 and 100 !!!");

        ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);

        // An EncoderParameters object has an array of EncoderParameter objects. 
        // In this case, there is only one EncoderParameter object in the array.  
        var myEncoderParameters = new EncoderParameters(1);

        var myEncoderParameter = new EncoderParameter(Encoder.Quality, quality);
        myEncoderParameters.Param[0] = myEncoderParameter;
        image.Save(destPath, jpgEncoder, myEncoderParameters);
    }

    ImageCodecInfo GetEncoder(ImageFormat format)
    {
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
        foreach (ImageCodecInfo codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }
        return null;
    }

    SaveImage(new Bitmap(srcPath), destPath, 85L);

What is happening is that your JPEGs are compressed.发生的事情是您的 JPEG 被压缩。 When you load them into memory they are uncompressed.当您将它们加载到 memory 时,它们是未压缩的。 This uncompressed size is definitely larger than the original JPG.这个未压缩的大小肯定比原始 JPG 大。

When you save the image, you are not compressing from the original size but from the larger uncompressed image in memory.保存图像时,您不是从原始大小压缩,而是从 memory 中较大的未压缩图像压缩。 The recompression process is not aware of the fact that the image it is compressing came from a compressed image originally.重新压缩过程不知道它正在压缩的图像最初来自压缩图像这一事实。 It just runs the algorithm on the image in memory at the compression setting you give.它只是在您提供的压缩设置下对 memory 中的图像运行算法。

This is why lossy formats like JPEG show compression artifacts after a few rounds of open/save.这就是为什么像 JPEG 这样的有损格式会在几轮打开/保存后显示压缩伪影。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM