减少图像大小imagemagic

时间:2016-09-28 14:44:12

标签: c# imagemagick magick.net

我试图压缩图片但没有成功。

看看我的小实验

            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

有人能指出我正确的方向吗?

我已在此处阅读了一些详细信息ImageMagick: Lossless max compression for PNG?

1 个答案:

答案 0 :(得分:4)

您似乎正在使用Magick.NET。该库有一个名为ImageOptimizer的类,可用于无损压缩文件。有关如何使用它的示例,请访问: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);

您的文件仍有可能无法缩小,因为它已经以最佳压缩方式存储。