August 16, 2013

An Animated WPF Gif

The Image control in WPF does not support animated GIF files by default. However, there is a library on codeplex, here, that can be used to do this.
Here is my usage of this library for an animated busy indicator.
In the XAML, first of all create an XML namespace for the library:
xmlns:gif="http://wpfanimatedgif.codeplex.com"
then use it with an image control:
<Image Name="imgBusy" 
       Stretch="UniformToFill" VerticalAlignment="Top" Width="20"  
    gif:ImageBehavior.AnimatedSource="/ScriptRunnerWPF;component/Resources/busyIndicator.gif" 
    gif:ImageBehavior.AutoStart="False" />
In code the animation of the GIF can be controlled:
Start the animation
   
this.imgBusy.Visibility = System.Windows.Visibility.Visible;
ImageAnimationController iac = ImageBehavior.GetAnimationController(this.imgBusy);
if (iac != null)
    iac.Play();
Resume the animation
// Resume the animation (or restart it if it was completed)
ImageAnimationController iac = ImageBehavior.GetAnimationController(this.imgBusy);
if (iac != null)
 iac.Pause();
this.imgBusy.Visibility = System.Windows.Visibility.Hidden;

No comments: