Animation of images using ImageList control in VB.NET
In this article you will learn about ImageList control and how this control is used to animate a series of images.
A visual basic ImageList control allows you to store images in to this control and reference them, almost like an array. this control is handy for Animation because it can be used alongside a Timer with a little code. The main advantage of using the ImageList control is that you can treat the images as a collection. Another advantage of the ImageList control is that the images are added into your project assembly for easy distribution and fast execution. If you want to display images, you have to add images using the Collection property of the ImageList control. Selecting a whole group of images and adding them at one time to the ImageList is a timesaving task. You will also set the image size and bit depth that applies to the whole collection of ImageList. When the First image is added to the collection, all other images must be the same height and width, otherwise an error will occur.
Here we are taking an example which shows how a ImageList control is used to animate a series of images. The images are stored in a ImageList control. When the program starts, it saves the number of Images. When the Timer fires, the program uses CreateGraphics to make a graphics object for the form. It uses the object's DrawImage method to display the next image on the form.
How to use ImageList control to animate a series of images:-
- Simply open a new project.
- Drag a ImageList and Timer control on form. The form will look like below.
- Now go to properties of the imagelist and using property collection add images which you want to animate.
- Write the below code on Form.
Private m_Index As Integer
Private m_NumImages As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
m_NumImages = ImageList1.Images.Count
m_Index = -1
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
m_Index = (m_Index + 1) Mod m_NumImages
Dim gr As Graphics = Me.CreateGraphics()
gr.DrawImage(ImageList1.Images(m_Index), 10, 10)