WPF: Playing GIFs Is Frustrating!

WPF: Playing GIFs Is Frustrating!

Today, I introduce a library for displaying animated GIFs in WPF, which can be used in XAML or code: `WpfAnimatedGif`.

Last updated 7/2/2021 10:12 PM
沙漠尽头的狼
2 min read
Category
WPF
Topic
WPF Open Source Projects
Tags
.NET WPF WPF Open Source Projects Open Source GIF

Tired of playing GIFs in WPF?

WpfAnimatedGif

Repository: https://github.com/XamlAnimatedGif/WpfAnimatedGif

NuGet package: WpfAnimatedGif.

Today, I’m introducing a library for displaying animated GIF images in WPF, usable in XAML or code: WpfAnimatedGif.

Simple and easy to use: In XAML, use the AnimatedSource attached property to set the GIF you want to display (replacing the Source property):

<Window
  x:Class="WpfAnimatedGif.Demo.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:gif="http://wpfanimatedgif.codeplex.com"
  Title="MainWindow"
  Height="350"
  Width="525"
>
  <Grid> <image gif:ImageBehavior.AnimatedSource="Images/animated.gif" /></Grid
></Window>

You can also specify the repeat behavior (default is 0x, which means it will use the repeat count from the GIF metadata):

<image
  gif:ImageBehavior.RepeatBehavior="3x"
  gif:ImageBehavior.AnimatedSource="Images/animated.gif"
/>

Of course, you can also set the GIF image in code:

var image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(fileName);
image.EndInit();
ImageBehavior.SetAnimatedSource(img, image);

For more details on usage, see the wiki.

Features

  • No new controls added; extends the native WPF Image control with attached properties for dynamic GIF loading
  • Respects actual frame durations
  • Allows specifying repeat behavior; if not specified, uses the repeat count from GIF metadata
  • Notifies when animation playback completes, enabling specific actions after the animation ends
  • Animation preview in design mode (must be explicitly enabled)
  • Supports manual control of animation (pause/resume/seek)
Keep Exploring

Related Reading

More Articles
Same category / Same tag 9/13/2025

Migration Series from WPF to Avalonia: Why I Must Migrate My WPF Application to Avalonia

In the past few years, our host computer software has mainly been developed using WPF and WinForm . These technologies work well on the Windows platform and have accompanied us from small-scale trial production to the current stage of large-scale delivery. However, with business development and changes in customer requirements, the single Windows technology stack has gradually become a hurdle we must overcome.

Continue Reading
Same category / Same tag 1/26/2025

Implementing Internationalization in WPF Using Custom XML Files

This article details the method of implementing internationalization in WPF applications using custom XML files, including installing the necessary NuGet packages, dynamically retrieving the language list, dynamically switching languages, using translated strings in code and XAML interfaces, and provides a source code link to help developers easily achieve internationalization in WPF applications.

Continue Reading