Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize an animated gif image using C#?

Is there a method to create a copy of an animated gif image using C#?

What I want is to generate a copy of a given gif image using the height and width parameters that the user provides. I have tried for a couple of hours to accomplish this but the resulting image does not preserve the original animations.

like image 826
Jaime Febres Avatar asked Feb 11 '09 22:02

Jaime Febres


1 Answers

Took me a while to find this, but finally found a solution:

Install Magick.NET via NuGet, license can be found here:
https://magick.codeplex.com/license

Example code:

var newWidth = 100;
using (var collection = new MagickImageCollection(new FileInfo(@"C:\test.gif")))
{
    collection.Coalesce();
    foreach (var image in collection)
    {
        image.Resize(newWidth, 0);
    }
    collection.Write(@"c:\resized.gif");
}

From my tests, this works with alpha channels and varying frame rates. Seems to be perfect!

like image 67
Tom Gullen Avatar answered Sep 19 '22 16:09

Tom Gullen