Click here to Skip to main content
11,929,232 members (48,413 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked

Another approach to animated GIF in WPF, with transparency

, 31 Oct 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Demonstrates the use of animated GIFs in WPF applications.



In spite of reach support of animation in WPF, I have not found a quick and easy way to insert animated GIFs in a WPF application. After some searching in the net and analyzing the gathered information, I have assumed that every public available solution has one ore more of the following disadvantages:

  1. Use of unsafe code (yes, back to good old BitBlt!)
  2. Thread unsafe.
  3. No or incomplete support of WPF layout, stretching, zoom, etc.
  4. No support for transparency.
  5. Not supported in XAML.
  6. Impossible to use “embedded” resources (only external GIF files supported).
  7. Too complicated and “heavy” design (for example, browser control with dynamically generated content, etc.)

This article presents my trial to solve the problem, taking into account the above mentioned issues. Hope that a future version of the framework brings us such functionality by default.

How it works and design of the code

The main idea is just simple: extend the standard WPF Image control to accept animated GIF, split the GIF into frames, constructing BitmapSources from each frame and keeping them “inside”, then dynamically switching sources to simulate animation.

Let’s look a bit deeper in code: I have created a custom control AnimatedImage, based on the WPF Image control, with an additional Dependency Property AnimatedBitmap of type Bitmap. This saves the full functionality of the original WPF image like scaling, layout, events etc., and makes it easy to set everything directly from XAML. The custom routed event AnimatedBitmapChanged was registered to provide some extra initialization/cleanup during setting the source GIF, if necessary. Actually, the main functionality is concentrated in the following function:

private void UpdateAnimatedBitmap()
  int nTimeFrames = AnimatedBitmap.GetFrameCount
  _nCurrentFrame = 0;
  if (nTimeFrames > 0)
    _BitmapSources = new BitmapSource[nTimeFrames];
    for (int i = 0; i < nTimeFrames; i++)
           System.Drawing.Imaging.FrameDimension.Time, i);
      Bitmap bitmap = new Bitmap(AnimatedBitmap);
      _BitmapSources[i] = 

Here, the frames are retrieved from the source GIF, then each frame is made transparent, converted to a BitmapSource, and saved into an internal buffer. Finally, we call the StartAnimate() helper to start the animation process. For controlling animation, an internal ImageAnimator control is used, with the ChangeSource() callback, doing nothing but just switching the active source of the Image control and sending an update appearance request. The BeginInvoke used here is for thread safety.

private delegate void VoidDelegate();

private void OnFrameChanged(object o, EventArgs e)
        new VoidDelegate(delegate { ChangeSource(); }));

void ChangeSource()
  Source = _BitmapSources[_nCurrentFrame++];
  _nCurrentFrame = _nCurrentFrame % _BitmapSources.Length;

As I mentioned, there are also two helpers StartAnimate() and StopAnimate() and a property IsAnimating to give the basic control over the AnimatedImage.

public void StopAnimate()
  if (_bIsAnimating)
         new EventHandler(this.OnFrameChanged));
    _bIsAnimating = false;

public void StartAnimate()
  if (!_bIsAnimating)
                  new EventHandler(this.OnFrameChanged));
    _bIsAnimating = true;

private bool _bIsAnimating;
public bool IsAnimating
   get { return _bIsAnimating; }

Using the code

Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.

Step 1a. Using this custom control with sources:

  • Add the WpfAnimatedControl project to your workspace.
  • Add the XmlNamespace attribute to the root element of the markup file where it is to be used:
  • xmlns:MyNamespace="clr-namespace:WpfAnimatedControl"

Step 1b. Using this custom control as DLL:

Add the XmlNamespace attribute to the root element of the markup file where it is to be used:


You will also need to add a project reference from the project where the XAML file lives to this assembly, and Rebuild to avoid compilation errors.

Step 2. Go ahead and use your control in the XAML file.

To add AnimatedImage to a window, insert the following line to your window XAML file, for example, as shown below:

< MyNamespace:AnimatedImage Name="aimg" 
    AnimatedBitmap="{x:Static code:Resources.wrong}" 
    Stretch="None" AnimatedBitmapChanged="aimg_AnimatedBitmapChanged"/>

Here we assume that you already insertedthe GIF in the window resources.

The provided test application (VS2008, Framework 3.5) demonstrates the common use of the control, and also setting the GIF from an external file.


Be careful to set the Source property together with the AnimatedBitmap property from XAML; generally, it was designed to use Source only if no animation runs. I.e., if you need a static picture, better use the original WPF Image, and do not set the Source if you are using the AnimatedImage control.


This is only a very basic implementation of an animated image control, but could save a lot of time if you need to “just show a transparent animated GIF”. Feel free to extend it with extra validation, functionality, and so on.


  • 31.10.2008: Initial release.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionBinding error Pin
devvvy15-Jun-15 18:31
memberdevvvy15-Jun-15 18:31 
QuestionMemory leak Pin
Member 331922520-Jan-15 4:46
memberMember 331922520-Jan-15 4:46 
QuestionUse of pack uri Pin
bhaash19-Sep-13 23:52
memberbhaash19-Sep-13 23:52 
BugIgnores the frame's display interval. Pin
tehcrash21-Jul-12 20:08
membertehcrash21-Jul-12 20:08 
GeneralMy vote of 5 Pin
Rupesh Kumar Swami17-Mar-12 1:45
memberRupesh Kumar Swami17-Mar-12 1:45 
QuestionSpeed problem Pin
Member 471711425-Feb-12 19:40
memberMember 471711425-Feb-12 19:40 
Generalthanx Pin
manura198526-Jan-12 21:41
membermanura198526-Jan-12 21:41 
GeneralMy vote of 5 Pin
Member 471711414-Jan-12 19:16
memberMember 471711414-Jan-12 19:16 
GeneralMy vote of 5 Pin
Guenter Wolf26-Apr-11 11:24
memberGuenter Wolf26-Apr-11 11:24 
GeneralError 18 Unknown build error, 'Key cannot be null Pin
pevans432114-Nov-10 15:43
memberpevans432114-Nov-10 15:43 
GeneralLeaking? [modified] Pin
chaf270128-Apr-09 6:51
memberchaf270128-Apr-09 6:51 
GeneralThreading issues... Pin
rlrcstr6-Mar-09 18:19
memberrlrcstr6-Mar-09 18:19 
GeneralRe: Threading issues... Pin
Lecha6-Apr-09 2:49
memberLecha6-Apr-09 2:49 
GeneralThank you Lecha. Here is a modified version to support both animated and stills. [modified] Pin
fnorum2-Jan-09 8:34
memberfnorum2-Jan-09 8:34 
GeneralRe: Thank you Lecha. Here is a modified version to support both animated and stills. [modified] Pin
Member 471711416-Feb-12 8:40
memberMember 471711416-Feb-12 8:40 
GeneralRe: Thank you Lecha. Here is a modified version to support both animated and stills. [modified] Pin
Member 471711417-Feb-12 3:07
memberMember 471711417-Feb-12 3:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 31 Oct 2008
Article Copyright 2008 by Lecha
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid