缩小图像大小 [英] Reduce image size imagemagic
问题描述
我尝试压缩图像,但是没有成功.
I have tried to compress image but has no success.
看看我的小实验
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?
我已经在这里阅读过一些详细信息 ImageMagick:PNG的无损最大压缩吗?
I have already read some details here ImageMagick: Lossless max compression for PNG?
推荐答案
似乎您正在使用 Magick.NET 一个>.该库有一个称为ImageOptimizer的类,可用于无损压缩文件.有关如何使用它的示例,可以在这里找到: https ://github.com/dlemstra/Magick.NET/blob/master/Documentation/LosslessCompression.md .
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.
这篇关于缩小图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!