Here's an example of one way to write the code to use these methods to display an animated GIF in a form:
Dim myAnimImage As Image
Private Sub VBAnimation_Load( _
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Load
Dim myAnimationFile As String = _
"C:\AboutVBAnim.gif"
myAnimImage = Image.FromFile(myAnimationFile)
End Sub
Protected Overrides Sub OnPaint( _
ByVal e As System.Windows.Forms.PaintEventArgs)
ImageAnimator.UpdateFrames()
e.Graphics.DrawImage(myAnimImage, New Point(0, 0))
End Sub
Private Sub StartAnim_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles StartAnim.Click
ImageAnimator.Animate(myAnimImage, _
New EventHandler(AddressOf onFrameChanged))
End Sub
Private Sub onFrameChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Me.Invalidate()
End Sub
Private Sub StopAnim_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles StopAnim.Click
ImageAnimator.StopAnimate(myAnimImage, _
New EventHandler(AddressOf onFrameChanged))
End Sub
If you want to use my animated GIF file to run the program code yourself, copy (right-click, select "Copy") and save the GIF file from the illustration above. The form used to display the GIF has just has two buttons named StartAnim and StopAnim as shown below:
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
The Load event just loads the GIF file into the myAnimImage Image object. The StartAnim and StopAnim buttons call the ImageAnimator.Animate and ImageAnimator.StopAnimate methods. But the key to the program is the onFrameChanged sub. Both the Animate and StopAnimate methods pass the address of the onFrameChanged subroutine. Microsoft states that this address must point to "an EventHandler object that specifies the method that is called when the animation frame changes." In this case, the "object" is just a subroutine with the right signature to receive the event ...
Private Sub onFrameChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs)
... and it only does one thing; invalidate the form so it can be repainted.
The Overrides Sub OnPaint sub then draws the image, but which image? An animated GIF contains a series of them. That's what the ImageAnimator.UpdateFrames method takes care of. It keeps track of which frame should be displayed next. If you have access to a program to create animated GIF's, you can prove to yourself that the ImageAnimator class will also read the delay time coded in the individual frames of the GIF and display them accordingly.
If you decide that you just need more, the next step "up the ladder" is to create actually unique images and display them with some sort of timer, such as System.Windows.Forms.Timer. You can create the individual images using the same kinds of techniques that previous segments of this tutorial have demonstrated. If you need this level of animation performance, your application will probably have lots of other requirements too such animation nested within animation. Consider, for example, a bird flying across the screen. The flapping of the wings is one level but the movement of the bird itself is a different level. Programming this level of animation requires analysis and careful coding, but the same techniques we have seen so far.
And if GDI+ itself just isn't good enough, the next step beyond that is Microsoft's DirectX technology. DirectX is a graphics technology that is implemented by the directly by the graphics processor in your computer, not just your CPU chip. This is the way high end graphics for both games and business processes like engineering workstations is handled and it's a totally different world from GDI+!

