2

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?

Community
  • 1
  • 1
XUser
  • 43
  • 1
  • 4

1 Answers1

5

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/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);

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

Roi Shabtai
  • 2,981
  • 2
  • 31
  • 47
dlemstra
  • 7,813
  • 2
  • 27
  • 43
  • Is there any disadvantage to using this? Do I just get lower file sizes, with no problem? I think that since it is lossless, I am not losing anything :-) but I am suspicious of how this can be possible. – pgr Jul 09 '19 at 09:42