Click here to Skip to main content
12,500,835 members (47,314 online)
Click here to Skip to main content
Add your own
alternative version

Stats

99.2K views
4.1K downloads
24 bookmarked
Posted

GIF animation in WPF

, 14 Feb 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

License

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

Share

About the Author

Zolador
Hungary Hungary
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralError in Code Pin
Rahul Singla16-Mar-09 7:00
memberRahul Singla16-Mar-09 7:00 
GeneralAn alternate solution Pin
Rob Reiss27-Feb-08 7:59
memberRob Reiss27-Feb-08 7:59 
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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160919.1 | Last Updated 14 Feb 2008
Article Copyright 2008 by Zolador
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid