saucecontrol / PhotoSauce

MagicScaler high-performance, high-quality image processing pipeline for .NET
http://photosauce.net/
MIT License
569 stars 49 forks source link

Support for animated webp images #91

Closed mortb closed 1 year ago

mortb commented 1 year ago

I try to scale / compress an animated webp and it becomes a static image. I tried to set output to GIF since GIF supports animations. No luck. Here is example code I used to download an animated webp image and compress it as a gif:

var http = new HttpClient();

var resp = await http.GetAsync("https://mathiasbynens.be/demo/animated-webp-supported.webp");
var ms = new MemoryStream();

var stream = await resp.Content.ReadAsStreamAsync();
stream.CopyTo(ms);
ms.Position = 0;

var settings = new ProcessImageSettings()
{
    Width = 320, 
    Height = 320,
    ResizeMode = CropScaleMode.Contain, // keep aspect ratio
};

settings.TrySetEncoderFormat(ImageMimeTypes.Gif);
settings.EncoderOptions = new GifEncoderOptions {
    MaxPaletteSize = 256
};

var filename = @$"c:\temp\compressed_{DateTime.Now.Ticks}.gif";
var compressed = new FileStream(filename, FileMode.Create);

await Task.Factory.StartNew(() => MagicImageProcessor.ProcessImage(ms, compressed, settings));

Process.Start(new ProcessStartInfo { FileName = filename, UseShellExecute = true });