Handling Animations and Bitmaps Using GDI+ for Image Manipulation - Image Animation in the Real World
(Page 4 of 4 )
Now let's implement the animation control into the application that was being built during previous parts of this series. The enhancements will provide the following functionalities:
- Start the animation if the GIF contains time based frames.
- Stop the animation.
The following are the menus that correspond to the functionalities:
- mnuAnimationStart
- mnuAnimationStop
The handler for the mnuAnimationStart is as follows:
private void mnuAnimationStart_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");
}
The handler first checks whether the image can be animated or not. Then it calls the Animate method.
The code for the mnuAnimationStop handler is as follows:
private void mnuAnimationStop_Click(object sender,
System.EventArgs e)
{
if(curImage != null)
{
ImageAnimator.StopAnimate(curImage,
new EventHandler(this.OnFrameChanged));
}
}
Add the following code to the OnPaint :
if(curImage != null)
{
if( ImageAnimator.CanAnimate(curImage) )
{
ImageAnimator.UpdateFrames();
}
}
Here I am checking whether or not the image contained in the curImage object has frames, because the rendering of other images also happens in OnPaint.
And here is the event handler referenced by the Animate and StopAnimate methods:
private void OnFrameChanged(object o, EventArgs e)
{
this.Invalidate();
}
Whenever the animation has to be started or stopped (in other words, the next frame has to be rendered), OnFrameChanged is called internally by the Animate and StopAnimate methods, which in turn invalidate the current drawing region.
This brings us to the end of the discussion on animation control and the basics of handling BMP files. In the next part I will discuss advanced aspects of BMP images and other image manipulation techniques such as skewing. Till then...
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |