Click here to Skip to main content
Licence CPOL
First Posted 14 Feb 2008
Views 67,740
Downloads 1,353
Bookmarked 18 times

GIF animation in WPF

By Zoltan23 | 14 Feb 2008
Animation of a transparent GIF image in WPF.
2 votes, 25.0%
1

2

3
3 votes, 37.5%
4
3 votes, 37.5%
5
3.38/5 - 8 votes
μ 3.38, σa 2.95 [?]

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)

About the Author

Zoltan23



Hungary Hungary

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralError in Code PinmemberRahul Singla8:00 16 Mar '09  
GeneralAn alternate solution PinmemberRob Reiss8:59 27 Feb '08  
GeneralRe: An alternate solution Pinmemberreinux23:40 23 Aug '08  
GeneralRe: An alternate solution PinmemberJeremyAC5:02 23 Sep '09  
GeneralAnother (better) solution PinmemberZoltan232:20 15 Feb '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120209.1 | Last Updated 14 Feb 2008
Article Copyright 2008 by Zoltan23
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid