Saving an animated Gif Image with c# in Windows Forms

As mentioned yesterday, you can use the GifEncoder from the Bumpkit class library to save animated Gif image files. In fact, the encoder has no dependencies on the rest of the class library, so I just copied it’s code-file into my solution.

Again, the source, for this and a whole bunch of other stuff, is here: RomyView.zip

I added a simple extension method, to allow saving collections of images to animated Gifs, much the same as my extension method that saves images to icon files. The extension method is just this:

Images to animated Gif extension method
  1. public static void SaveAnimatedGifImage(this IEnumerable<Image> images, string path)
  2. {
  3.     var imageArray = images.ToArray();
  4.    
  5.     using (var stream = new MemoryStream())
  6.     {
  7.         using (var encoder = new BumpKit.GifEncoder(stream))
  8.         {
  9.             for (int i = 0; i < imageArray.Length; i++)
  10.             {
  11.                 var image = (imageArray[i] as Bitmap).CopyImage();
  12.                 encoder.AddFrame(image, 0, 0, TimeSpan.FromSeconds(0));
  13.             }
  14.  
  15.             stream.Position = 0;
  16.             using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, Constants.BufferSize, false))
  17.             {
  18.                 stream.WriteTo(fileStream);
  19.             }
  20.         }
  21.     }
  22. }

 

Remarks: Perhaps it is worth noting that the CopyImage call above is just an extension method, similar to Bitmap.Clone. The code will fail to add frames without it, or something similar. I would also normally have added an async method the same as the above, but due to a nasty case of the flu, have done only the minimum for a change. (If you have paid attention in previous posts, you should see how easy it would be to add an async overload. All that’s required really is to mark the method async, then use an async method rather than Stream.WriteTo as the last step. i.e. Stream.CopyToAsync, after changing the FileStream constructor to support async operations.)

And here is an image I created with it (after exporting some frames from the trailer for the new man of Steel movie with ffmpeg).

I have scaled this down and set it to repeat only once due to it messing up the page load time.

Man of Steel

Note also, the VideoConverter part of my application was used to export the frames. (It just wraps the ffmpeg console application for Windows.) I just chose “Image Sequence” as the output format. It also allows you to crop and scale the output. Cropping was required here because the Man of Steel trailer features black areas above and below the actual image frames. I can’t tell you how to download videos from YouTube though. (I use a program written by someone else for that.)

Advertisements

About Jerome

I am a senior C# developer in Johannesburg, South Africa. I am also a recovering addict, who spent nearly eight years using methamphetamine. I write on my recovery blog about my lessons learned and sometimes give advice to others who have made similar mistakes, often from my viewpoint as an atheist, and I also write some C# programming articles on my programming blog.
This entry was posted in Programming and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s