Creating Animated GIF files in WPF

    General discussion

  • Hi,

    I just posted a short article on my blog describing how to create an animated GIF file from snapshots of WPF FrameworkElement objects. I have to add that the code isn't complete, and that's partly why I post here. I'm using RenderTargetBitmap and GifBitmapEncoder objects to get this to work, but the final .gif file has a flaw. When displayed (at least in IE7), the prior frame isn't erased before the next frame is rendered.

    The concept I present is very useful (in my opinion ). For instance, this approach can be used for showcasing WPF animations to viewers who don't have .NET 3.0 runtime installed.

    It would be great if anyone has suggestions for how to improve on the code, and solve the animation flaw.



    Koda Software

    P.S. If you are interested in working on WPF, please take a look at our current job openings.

    Sunday, December 17, 2006 12:33 AM

All replies

  • Unfortunately we don't expose a metadata reader or writer for GIFs (animated or otherwise). In order to produce correct animated GIFs, you need to write out some metadata with timing information. Since we don't provide a GIF metadata handler, we do provide a mechanism for 3rd parties to write their own. Documentation for writing a metadata handler can be found here: as well as in this article
    Tuesday, December 19, 2006 9:53 PM
  • Hi Robert,

    Thanks for the explanation of the situation with the animated GIFs, even if the technical details are a bit over my head.

    One thing that is confusing me is the fact that when I attempt to load an animated gif in page context, it simply displays the first frame (as expected based on your explanation above). However, if I simply drop the gif file in an IE7 window, the browser plays it just fine.

    Could you provide:
         (1) an explanation for this discrepancy?
         (2) a solution for creating the necessary metadata that is targeted more towards a web designer's level of technical expertise (i.e. some sort of third-party app or GUI that could be used to build this metadata)?

    Again, thanks for the information. It's a much need clarification.


    Thursday, January 04, 2007 5:39 PM
  • You can create multi-frame GIFs, but there won't be any timing information in the metadata. Perhaps GIF has some "default" timing if none is found in the file (in which case is why this may work in IE)... I'll need to read through the GIF spec more thoroughly to see if this is the case. Displaying a multi-frame or animated GIF using the <Image ... /> in WPF will only display the first frame. For one, the Source property takes a single BitmapSource rather than a collection of them. Even with a collection of BitmapSources, it'd be hard to animate through them without timing information. Some folks have had luck "playing" animated GIFs through the <MediaElement ... /> in WPF.
    Thursday, January 04, 2007 9:51 PM