Search In Site

01 July, 2013

How to Create Image Animation in visual Basic


Creating Image Animation
One way to create image animation is to use a picture box and keep changing its Picture property to display successive frames of an animation. You can store the images themselves in the program, such as using an image list control or an array of Picture objects. We’ve seen how to create animation earlier in this book in our chapter on Visual Basic timers using image lists; here, we can do the same thing using an array of Picture objects.
We add a timer control, Timer1, to the program and set its Interval property to 1000 (the Interval property is measured in milliseconds, 1/1000s of a second), which means the Timer1_Timer() event handler will be called once a second. We also add a picture box, Picture1, in which to display images and a command button, Command1, with the caption “Start animation” to start the animation.For the purposes of this example, we will just switch back and forth between two images in the picture box. These two images are the two images in the Picture object array, picObjects, which we store in the form’s
(General) section:
Dim picObjects(1 To 2) As Picture
We load those images when the form first loads:
Private Sub Form_Load()
Set picObjects(1) = LoadPicture("c:\vbbb\pictureanimation\image1.bmp")
Set picObjects(2) = LoadPicture("c:\vbbb\pictureanimation\image2.bmp")
End Sub
To switch back and forth, we use a static Boolean flag named blnImage1 like this, alternating between images
in the Picture object array in Timer1_Timer:
Private Sub Timer1_Timer()
Static blnImage1 As Boolean
If blnImage1 Then
Picture1.Picture = picObjects(1)
Else
Picture1.Picture = picObjects(2)
End If

At the end of Timer1_Timer, we toggle the blnImage1 flag this way:
Private Sub Timer1_Timer()
Static blnImage1 As Boolean
If blnImage1 Then
Picture1.Picture = picObjects(1)
Else
Picture1.Picture = picObjects(2)
End If
blnImage1 = Not blnImage1
End Sub

All that’s left is to start the animation when the user clicks the command button, and we do that like this, by 
enabling the timer:



Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
And that’s all we need—now we’re supporting animation using picture boxes and Picture object arrays.

0 comments:

Post a Comment

Dear Visitors All The Tricks And Hacks Posted Here Are Only For Knowledge Purpose.Don't Use These for Illegal Operations.

 
Twitter Bird Gadget