Handling Animations and Bitmaps Using GDI+ for Image Manipulation - Playing with Animation: StopAnimate and UpdateFrame
(Page 2 of 4 )
Just as the name suggests, the StopAnimate method stops the animation; in effect, it's the reverse of StartAnimate(). The parameters accepted by this method are the same as those for StartAnimate, namely the image to animate and the handler to be called when the frame changes. The following code illustrates the point:
if(curImage != null)
{
ImageAnimator.StopAnimate(curImage,
new EventHandler(this.OnFrameChanged));
}
where curImage is the reference of the image to be animated and OnFrameChanged is the handler to be called which is define as
private void OnFrameChanged(object o, EventArgs e) {
//Force a call to the Paint event handler.
this.Invalidate();
}
UpdateFrames is used to prepare the next frame for rendering. There are two versions of the method. One accepts an image reference as a parameter, while the other version doesn't accept any parameter. If the former is used, only the frame in the reference image is advanced i.e. prepared for rendering. When the latter is used, it advances the frame in all images currently being animated i.e. if there is more than one image being animated, frames in all the images are prepared for rendering. The actual rendering is done when the next Paint event occurs. The following code explains the point:
protected override void OnPaint(PaintEventArgs e) {
//Begin the animation.
//call the method to start animation
//Get the next frame ready for rendering.
ImageAnimator.UpdateFrames();
//Draw the next frame in the animation.
e.Graphics.DrawImage(this.animatedImage, new Point(0, 0));
}
Once the Animate method is called, the UpdateFrames method is called to prepare the next frame for animation.
That brings us to the end of this section. In the next section the focus will be on the basics of bitmap manipulation.
Next: Working With Bitmaps >>
More Code Examples Articles
More By A.P.Rajshekhar