This project has moved. For the latest updates, please go here.

Implement a resize for animated gif.

May 16, 2013 at 1:16 PM
It would be nice to have a way to resize animated gif. I've just tried to resize one and it lost the animation.

Here is the code that I used.
ImageMagick.MagickNET.Initialize();
ImageMagick.MagickImage image = new ImageMagick.MagickImage("myPath");
image.Resize(62, 62);
image.Write("myPath");
BTW, nice job and I tip my hat to you.
Coordinator
May 16, 2013 at 2:32 PM
Edited May 16, 2013 at 3:23 PM
You should use the MagickImageCollection class. There is however a bug that will prevent you to do this easily. I will try to publish a new version next week and resolve this issue.
// This should work.
using (MagickImageCollection collection = new MagickImageCollection("myPath"))
{
  for (int i = 0; i < collection.Count; i++)
  {
    // This does not work and needs a bugfix.
    collection[i].Resize(62, 62);
  }
  
  collection.Write("myPath");
}

// This is a temporary workaround.
using (MagickImageCollection input = new MagickImageCollection("myPath"))
{
  using (MagickImageCollection output = new MagickImageCollection())
  {
    for (int i = 0; i < input.Count; i++)
    {
      MagickImage image = input[i];
      image.Resize(62, 62);
      output.Add(image);
    }
  }
  
  output.Write("myPath");
}
May 16, 2013 at 5:31 PM
OOAAHH, It does work and the image even display correctly in Internet Explorer!!!
Also, the quality of the image is fantastic.

However, the output variable should be in the using section :)
using (MagickImageCollection input = new MagickImageCollection("myPath"))
{
    using (MagickImageCollection output = new MagickImageCollection())
    {
        for (int i = 0; i < input.Count; i++)
        {
            MagickImage image = input[i];
            image.Resize(62, 62);
            output.Add(image);
        }

        output.Write("mySavePath");
    }
}
Seriously, you're awesome!

Thanks,
Coordinator
May 16, 2013 at 9:50 PM
Edited May 19, 2013 at 10:34 PM
The problems with MagickImageCollection have been resolved with changeset 26274. When i have published the next release you can resize the animated gif this way:
using (MagickImageCollection collection = new MagickImageCollection("myPath"))
{
  foreach(MagickImage image in collection)
  {
    image.Resize(62, 62);
  }
  
  collection.Write("myPath");
}
Jan 9, 2015 at 6:26 AM
Edited Jan 9, 2015 at 6:28 AM
Thanks for the code it helped me a lot.

I'm having one problem though, the code works most of the time, but some animated gifs (check the link below for an example) doesnt resize properly using this code. Here's the original image:

http://www.thisiscolossal.com/wp-content/uploads/2014/03/120430.gif

I'm not sure if its the meta-data thats causing this, but its giving me a problem. Is there anyway around this?

Thanks!
Coordinator
Jan 9, 2015 at 7:09 AM
You will get better results if you use the code below. Normally a frame has optimizations that causes it to contain only the changes to the previous image. The call to Coalesce will remove the optimization and change the image to how it looks at that point during the animation. You can find a good explanation here: http://www.imagemagick.org/Usage/anim_basics/#coalesce
using (MagickImageCollection collection = new MagickImageCollection("myPath"))
{
  collection.Coalesce();

  foreach(MagickImage image in collection)
  {
    image.Resize(62, 62);
  }
  
  collection.Write("myPath");
}
Jan 9, 2015 at 7:15 AM
Thank you so much! That did the trick!
Coordinator
Jan 9, 2015 at 7:23 AM
Your welcome. I will also add this script to the examples.