Search code examples
c#imagemagickimagemagick-convertapngimagemagick.net

How to convert APNG into WEBP by using Magick.NET (imagemagick) and C#?


I have tried like this but APNG animation is lost

Also I am looking for settings to lossless yet maximum compression. I don't care about CPU time speed etc. I need lossless and maximum compression. how to achieve that?

So I have a lot of PNG files and some APNG files

I will turn all of them into WEBP files

Yet I still want to keep APNG animation as well

Here example APNG to test : https://static.pokemonpets.com/images/npcTrainers/trainer15125down.png

     using (var image = new MagickImage(@"path\Debug\a.png"))
        {
            image.Quality = 100;
            image.Settings.SetDefine(MagickFormat.WebP, "lossless", true);
            image.Settings.SetDefine(MagickFormat.WebP, "method", "6");
            image.Write("a.webp");
        }

C# .net framework 4.8, Magick.NET-Q16-AnyCPU, 7.24.1.0


Solution

  • If you want to read an image that contains multiple frames you will need to use the MagickImageCollection instead:

    using (var images = new MagickImageCollection(@"apng:path\Debug\a.png"))
    {
        var defines = new WebPWriteDefines
        {
            Lossless = true,
            Method = 6,
        };
    
        images.Write("a.webp", defines);
    }
    

    And if you want to read an animated PNG file you will need to have ffmpeg.exe in your %PATH%. This is required to read an animated PNG file. And you will also need to add apng: to make sure that ImageMagick knows that it is an animated PNG file.