10.16 Loading and Displaying Animated GIF s

 <  Day Day Up  >  

10.16 Loading and Displaying Animated GIF's

You want to determine whether a GIF contains animation frames and display it on a Windows Form.


Technique

The ImageAnimator class determines whether an image contains animation frames as well as controls when the image should be updated to display the next frame. To determine whether an image is animated, use the static method CanAnimate defined in the ImageAnimator class, passing an Image object instance. To start the animation, call the static method Animate , passing the Image object in the first parameter and an EventHandler delegate as the second parameter:

 
 private void menuOpen_Click(object sender, System.EventArgs e) {     if( openFileDialog1.ShowDialog() == DialogResult.OK )     {         image = Image.FromFile( openFileDialog1.FileName );         this.Text = "Image Test - " + openFileDialog1.FileName;         // check if animated image         if( ImageAnimator.CanAnimate( image ) == true )         {             animating = true;             ImageAnimator.Animate(image, new EventHandler(this.OnImageAnimate));         }         else         {             animating = false;         }         Invalidate();     } } private void OnImageAnimate(object o, EventArgs e) {     //Force a call to the Paint event handler.     this.Invalidate(); } 

The ImageAnimator is responsible for ensuring that each frame within an animated image is selected as the current frame. To move to the next frame and render an image, call the UpdateFrames static method, passing the Image object to update. Rendering the image itself is similar to rendering other Image objects, which you accomplish by calling the Graphics method DrawImage :

 
 protected override void OnPaint(PaintEventArgs e) {     if( image == null )     {         return;     }     if( animating == true )         ImageAnimator.UpdateFrames();     e.Graphics.DrawImage( image, new Rectangle(0,0,image.Width, image.Height), 0, 0,                           image.Width, image.Height, GraphicsUnit.Pixel ); } 

Comments

GDI+ supports several different file formats, including animated GIF's. An animated GIF contains several frames within a single file that you can enumerate on a time interval to render the animation. However, simply calling the DrawImage method does not cause an animated GIF to render on a frame-by-frame basis. In this case, only the first frame is rendered. In the code for this recipe, you can see how the ImageAnimator class updates the current frame pointer within an animated GIF. When the image is rendered using DrawImage , GDI+ looks to see what frame is currently selected and renders just that frame. You perform subsequent frame updates by calling the UpdateFrames method defined in the ImageAnimator class.

Just as simply calling DrawImage is not enough to render each frame within an animated GIF, calling the Save method defined in the Image class does not save all the frames within the image. It simply saves the first frame and ignores any subsequent frames. To save an animated GIF so that all its frames are preserved, you must follow the Save method call with a SaveAdd call for each frame within the image.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net