Click here to Skip to main content
13,091,145 members (66,457 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 13 Feb 2008

GIF animation in WPF

, 14 Feb 2008
Rate this:
Please Sign up or sign in to vote.
Animation of a transparent GIF image in WPF.


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.

   //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.


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


About the Author

Hungary Hungary
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralError in Code Pin
Rahul Singla16-Mar-09 7:00
memberRahul Singla16-Mar-09 7:00 
GeneralAn alternate solution Pin
Rob Reiss4-Mar-17 1:38
memberRob Reiss4-Mar-17 1:38 
GeneralRe: An alternate solution Pin
reinux23-Aug-08 22:40
memberreinux23-Aug-08 22:40 
GeneralRe: An alternate solution Pin
JeremyAC23-Sep-09 4:02
memberJeremyAC23-Sep-09 4:02 
GeneralAnother (better) solution Pin
Zoltan2315-Feb-08 1:20
memberZoltan2315-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 14 Feb 2008
Article Copyright 2008 by Zolador
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid