问题
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