Handling Animations and Bitmaps Using GDI+ for Image Manipulation
(Page 1 of 4 )
Among all the formats of image files, GIF (Graphic Interchange Format) and BMP occupy a special place. GIF can support animation, while BMP's bitmapped nature sets it apart. GDI+ provides APIs to control the animation contained in a GIF as well as manipulate the bitmaps in a BMP.
In this discussion I will focus on controlling animated GIF and the basics of manipulating BMP images using GDI+. The first and section sections will concentrate on the animated GIF and the APIs for controlling it. The third section will cover working with bitmapped images. Finally, in the fourth section I will enhance the application developed in the previous part by adding the functionality to play animations when the selected image is an animated GIF. That's the agenda for this discussion.
This article is the third part of a series. The reader may benefit from reviewing the first two parts, Basic Image Manipulation Using GDI+ and C# and Thumbnails and Zooming with GDI+ and C#.
Playing with Animation: CanAnimate and Animate
By definition, "Animation is the rapid display of a sequence of 2-D artwork or model positions in order to create an illusion of movement." The articles I have written until now dealt with the manipulation of static images.
There are formats that can hold data for animation. These are AVI, MPEG, GIF and others. The images in these formats are known as animated images, as in the animated GIF. Animated images hold three kinds of data: images that have to be used, known as frames, and the order as well as the interval between the frames.
Though there are multiple formats that can handle animation, GDI+ currently supports multi-frames only in TIFF and GIF. Out of these I will concentrate solely on GIF. The following are the APIs provided by GDI+ for the animated GIF through the ImageAnimator class :
- CanAnimate
- Animate
- StopAnimate
- UpdateFrames
All of these methods are static. GDI+ doesn't provide any support for the creation of animated images. In this section and the next I will focus on each of these methods in turn.
CanAnimate is the first method that needs to be called to ensure that the image can be animated. It returns true if the image contains time-based frames. This method takes the reference of the image to be tested for time-based frames as an argument. The point to remember is that any animated image will contain time based frames. If the time-based frames are not there, it is a static image. When a static image is given as an argument, false is returned by the method.
For example, the following code checks for the animation capability of an image:
curImage = Image.FromFile(curFileName);
if( ImageAnimator.CanAnimate(curImage) )
{
//logic for animating the image
}
else
MessageBox.Show("Image doesn't have frames");
Once it has been determined that an image contains time-based frames, then its animation can be controlled by controlling the frames. The Animate method is invoked to accomplish this goal. It takes two arguments, an image and an event handler. The image refers to the current image and the event handler is called when the frame changes. In other words, the Animate method controls the animation by triggering the event handler passed as an argument on the change of frames within the passed image parameter. In code it would be:
if (!currentlyAnimating) {
//Begin the animation only once.
ImageAnimator.Animate(animatedImage, new EventHandler(this.OnFrameChanged));
currentlyAnimating = true;
}
private void OnFrameChanged(object o, EventArgs e) {
//Force a call to the Paint event handler.
this.Invalidate();
}
In the above example, the Animate method is given two parameters. These are the animatedImage, which is the image to be animated, and an event handler referenced by this.OnFrameChanged. The handler invalidates the current rendered image to draw the new frame, thus providing an "animation."
Next: Playing with Animation: StopAnimate and UpdateFrame >>
More Code Examples Articles
More By A.P.Rajshekhar