7.4 Playing Animations in GDI+
So far we have been dealing with static image formats, such as BMP. Each of these formats holds image data for a single picture. Other formats -such as GIF, AVI (Audio Video Interleaved), and MPEG (Moving Picture Experts Group) -contain image data that, when
Figure 7.29. An animated image with three frames
You can create animated images by using graphics tools such as Macromedia Fireworks or CorelDRAW, but GDI+ doesn't support the creation of animated images. When you create animated images, you must specify the order of frames and the time interval between them.
The GDI+ library provides the
ImageAnimator
class to deal with animated file formats using time-based frames. At this time, GDI+ supports only
Now let's write an application that will play animated images. We create a Windows application and add a
MainMenu
control and two button controls to the form. We also add two menu items:
Open File
and
Exit
. We change the text and
Figure 7.30. An image animation example
We add two
private Image curImage = null; private string curFileName = null;
The
Open File
menu item allows us to browse images, and the
Exit
menu item
Listing 7.13 The Open File and Exit menu item click event handlers
private Image curImage; private void OpenFileMenu_Click(object sender, System.EventArgs e) { // Create OpenFileDialog OpenFileDialog opnDlg = new OpenFileDialog(); opnDlg.Filter = "Animated Gifs*.gif;"; // If OK, selected if(opnDlg.ShowDialog() == DialogResult.OK) { // Read current selected file
Now we rename the two
Listing 7.14 The StartAnimationBtn click event handler
private void StartAnimationBtn_Click(object sender, System.EventArgs e) { curImage = Image.FromFile(curFileName); if( ImageAnimator.CanAnimate(curImage) ) { ImageAnimator.Animate(curImage, new EventHandler(this.OnFrameChanged)); } else MessageBox.Show("Image doesn't have frames"); }
On the StopAnimationBtn click event handler, we check whether there is an Image object, and we call StopAnimate to stop the animation as shown in Listing 7.15. Listing 7.15 The StopAnimationBtn click event handler
private void StopAnimationBtn_Click(object sender, System.EventArgs e) { if(curImage != null) { ImageAnimator.StopAnimate(curImage, new EventHandler(this.OnFrameChanged)); } }
Now we add OnPaint and OnFrameChanged methods to the application. The code for these methods is given in Listing 7.16. In the OnPaint method, we call the UpdateFrames method of ImageAnimator and then call DrawImage to draw the image. In the OnFrameChanged method, we repaint the form by calling Invalidate . Listing 7.16 The OnPaint and OnFrameChanged methods
protected override void OnPaint(PaintEventArgs e) { if(curImage != null) { ImageAnimator.UpdateFrames(); e.Graphics.DrawImage(curImage, new Point(0, 0)); } } private void OnFrameChanged(object o, EventArgs e) { this.Invalidate(); }
Now compile and run the application. You can browse animated images on your system or download the files from online and select a file. The Start Animation button will start playing the animation. The Stop Animation button will stop the animation. Figure 7.31 shows the first frame of the animation sample provided with this book (download code from online). Figure 7.31. The first frame of an animated image
Figure 7.32 shows the second frame of the sample. Figure 7.32. The second frame of an animated image
|