This project has moved and is read-only. For the latest updates, please go here.

AnimationIterations issue

Jun 23, 2014 at 2:48 PM
Hi, trying to create an animated gif however there's something weird (at least from what I can tell) going on.
Can't get MagickImageCollection.Write to stream to work as it should, end result never gets animated.

Written a test console app that uses a list of filenames and renders an animated gif.
using (var collection = new MagickImageCollection())
            {
                // Add first image and set the animation delay to 200
                foreach (var file in sourceFiles.Where(x => x.EndsWith("png")))
                {
                    Trace.TraceInformation("ADDED: " + file);
                    var image = new MagickImage(file, new MagickReadSettings { Format = MagickFormat.Png }) { AnimationDelay = 200 };
                    collection.Add(image);
                }

                // Reduce colors
                var settings = new QuantizeSettings { Colors = 256 };
                collection.Quantize(settings);

                // Optimize the images
                collection.OptimizePlus();
So far so good.
If I then do a:
collection.Write("filename.gif");
gif seems to work fine.
If I instead write to a stream and then write the output to a gif file using a MagickImage. All I get seems to be the first frame? (previewing in chrome)
As you can see I do this to be able to set AnimationIterations for the gif. Any reason for that setting not being available on the MagickImageCollection object, so one could skip to load the output from collection to a magickimage? Using the latest stable release at the moment, fresh from nuget. 16 anycpu.
var gifStream = new MemoryStream();
collection.Write(gifStream);
gifStream.Position = 0;
using (var gif = new MagickImage(gifStream) { AnimationIterations = 5 })
{
    gif.Write("filename.gif");
}
Am I missing something or is there a bug?
Jun 23, 2014 at 3:13 PM
Of course I notice it just after having posted the above.
If I set format to gif on every frame it works.

Shouldn't it be possible to set image format on write when writing collection to a stream?
And shouldn't the AnimationIterations setting be available on a collection, so one wouldn't have to reload the gif stream into a MagickImage so set number of loops?

Best regards!
Jun 23, 2014 at 8:36 PM
Edited Jun 25, 2014 at 5:56 AM
I think you are correct and I will add an overload to the Write method that has Stream and MagickFormat as a parameter. I will also do this for MagickImage. You only need to set AnimationIterations on the first image. But it might be a good idea to add AnimationDelay / AnimationIterations / GifDisposeMethod to MagickImageCollection.

Edit: The format parameter has been added. I will not add the properties because it is trivial to set them:
var gifStream = new MemoryStream();
foreach (MagickImage image in collection)
{
  collection.AnimationIterations = 5;
  collection.AnimationDelay = 200;
  collection.GifDisposeMethod = GifDisposeMethod.Background;
}
collection.Write(gifStream);
gifStream.Position = 0;