2

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

看看我的小实验

            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:PNG 的无损最大压缩?

4

1 回答 1

5

看来您正在使用Magick.NET。该库有一个名为 ImageOptimizer 的类,可用于无损压缩文件。可以在此处找到有关如何使用它的示例:https ://github.com/dlemstra/Magick.NET/blob/main/samples/Magick.NET.Samples/LosslessCompression.cs

    var snakewareLogo = new FileInfo(SampleFiles.OutputDirectory + "OptimizeTest.jpg");
        File.Copy(SampleFiles.SnakewareJpg, snakewareLogo.FullName, true);

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

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

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

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

于 2016-09-28T18:58:53.733 回答