简体   繁体   English

减小 PNG 图像的大小

[英]Reduce the size of the PNG image

I want to compress PNG image我想压缩PNG图片

I am using below code: (Below code is working fine for jpeg,jpg) but not for png.我正在使用以下代码:(以下代码适用于 jpeg、jpg)但不适用于 png。

        var qualityParam = new EncoderParameter(Encoder.Quality,80);
        // PNG image codec 
        var pngCodec = GetEncoderInfo(ImageFormat.Png);
        var encoderParams = new EncoderParameters(1) { Param = { [0] = qualityParam } };

        rigImage.Save(imagePath, pngCodec, encoderParams);

        private static ImageCodecInfo GetEncoderInfo(ImageFormat format)
        {
          // Get image codecs for all image formats 
          var codecs = ImageCodecInfo.GetImageEncoders();

          // Find the correct image codec 
          return codecs.FirstOrDefault(t => t.FormatID == format.Guid);
         }

JPEG "works fine" because it can always discard more information. JPEG“工作正常”,因为它总是可以丢弃更多信息。

PNG is a lossless image compression format, so getting better compression is trickier and there's a pretty high "low mark". PNG 是一种无损图像压缩格式,因此获得更好的压缩更棘手,并且有一个相当高的“低分”。 There are tools like PNGOut or OxiPNG which exist solely to optimise PNG images, but most strategies are very computationally expensive so your average image processing library just doesn't bother:有像 PNGOut 或 OxiPNG 这样的工具,它们仅用于优化 PNG 图像,但大多数策略在计算上非常昂贵,因此您的普通图像处理库不会打扰:

  • you can discard irrelevant metadata chunk您可以丢弃不相关的元数据块
  • you can enumerate and try out various filtering strategies, this amounts to compressing the image dozens of time with slightly different tuning and checking out the best您可以列举并尝试各种过滤策略,这相当于使用略有不同的调整将图像压缩数十次并检查出最好的
  • you can switch out the DEFLATE implementation from the default (usually the stdlib or system's standard) to something better but much more expensive like zopfli您可以将 DEFLATE 实现从默认(通常是 stdlib 或系统标准)切换到更好但更昂贵的东西,如 zopfli
  • finally — and this one absolutely requires human eyeballs — you can try and switch to palletised最后——这个绝对需要人的眼球——你可以尝试切换到托盘化

As noted above, most image libraries simply won't bother with that, they'll use a builtin zlib/deflate and some default filter, it can takes minutes to make a PNG go through an entire optimisation pipeline, and there's a chance the gain will be non-existent.如上所述,大多数图像库根本不会为此烦恼,他们将使用内置的 zlib/deflate 和一些默认过滤器,使 PNG 通过整个优化管道可能需要几分钟的时间,并且有可能获得收益将不存在。

As @Masklinn said, PNG is a lossless format, and I think the BCL in .NET does not have an API that can help you to "optimize" the size of the PNG.正如@Masklinn 所说,PNG 是一种无损格式,我认为 .NET 中的 BCL 没有可以帮助您“优化”PNG 大小的 API。

Instead, you could use the libimagequant library;相反,您可以使用libimagequant库; you can get more information about this library here , and how to use it from .NET here .您可以在此处获得有关此库的更多信息,以及如何在此处从 .NET 使用它。

This is the same library used by PNGoo , I have got really impressive results with it when optimizing PNGs in my projects.这是PNGoo使用的同一个库,在我的项目中优化 PNG 时,我得到了非常令人印象深刻的结果。

Also, if you are planning to use the library in a commercial project, keep in mind the license terms, as indicated at https://pngquant.org/lib/ .此外,如果您计划在商业项目中使用该库,请记住许可条款,如https://pngquant.org/lib/ 所示

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

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