Reduce image size imagemagic

≯℡__Kan透↙ 提交于 2019-12-24 00:12:04

问题


I have tried to compress image but has no success.

Look at my small experiment

            var results = new Dictionary<int, int>();
            for (int i = 0; i <= 100; i++)
            {
                var stream = new MemoryStream();
                image.Quality = i;
                image.CompressionMethod = CompressionMethod.Zip;
                image.Write(stream, MagickFormat.Png);
                results[i] = stream.GetBuffer().Length;
                stream.Flush();
            }

            var best = results.OrderBy(e => e.Value).First(); 
           // the same length as for original image. quality doesn't work in this example - dictionary values are identical

Could any one point me to right direction?

I have already read some details here ImageMagick: Lossless max compression for PNG?


回答1:


It seems that you are using Magick.NET. That library has a class called ImageOptimizer that can be used to lossless compress the file. An example on how you could use that can be found here: https://github.com/dlemstra/Magick.NET/blob/master/Documentation/LosslessCompression.md.

FileInfo snakewareLogo = new FileInfo(@"c:\Snakeware.png");

Console.WriteLine("Bytes before: " + snakewareLogo.Length);

ImageOptimizer optimizer = new ImageOptimizer();
optimizer.LosslessCompress(snakewareLogo);

snakewareLogo.Refresh();
Console.WriteLine("Bytes after:  " + snakewareLogo.Length);

It is still possible that your file cannot be reduced in size because it is already stored with the best compression.



来源:https://stackoverflow.com/questions/39750795/reduce-image-size-imagemagic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!