Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / WPF

GIF animation in WPF

Rate me:
Please Sign up or sign in to vote.
3.41/5 (9 votes)
14 Feb 2008CPOL 124.7K   4.7K   25   6
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:

C#
public class AnimatedImageControl : UserControl {...} 

and overridden the OnRender function:

C#
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.

License

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


Written By
Hungary Hungary
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralError in Code Pin
Rahul Singla16-Mar-09 7:00
Rahul Singla16-Mar-09 7:00 
GeneralAn alternate solution Pin
Rob Reiss4-Mar-17 1:38
Rob Reiss4-Mar-17 1:38 
GeneralRe: An alternate solution Pin
Rei Miyasaka23-Aug-08 22:40
Rei Miyasaka23-Aug-08 22:40 
GeneralRe: An alternate solution Pin
JeremyAC23-Sep-09 4:02
JeremyAC23-Sep-09 4:02 
GeneralAnother (better) solution Pin
Zolador15-Feb-08 1:20
Zolador15-Feb-08 1:20 

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.