65.9K
CodeProject is changing. Read more.
Home

GIF animation in WPF

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.41/5 (9 votes)

Feb 14, 2008

CPOL
viewsIcon

125464

downloadIcon

4749

Animation of a transparent GIF image in WPF.

Introduction

I have found a solution for GIF animation by only using Windows Forms. Based on this, I have solved this problem in WPF (.NET 3.5).

Using the Code

I have created my own class:

public class AnimatedImageControl : UserControl {...} 

and overridden the OnRender function:

protected override void OnRender(DrawingContext p_drawingContext)
{ 
   //Get the next frame ready for rendering.
   ImageAnimator.UpdateFrames(m_animatedImage);

   //Draw the next frame in the animation.
   Graphics gr = Graphics.FromHwnd(m_hwnd.Handle);
   gr.FillRectangle(m_brush, m_rectangle);
   gr.DrawImage(m_animatedImage, m_rectangle);
}

Points of Interest

The main trick is using Graphics instead of DrawingContext.

It works properly only if the background is a solid color brush, because calling FillRectangle() is necessary.