Author: Shehzad Hemani
Download Source Code : 1094_source.zip
In this simple article you will learn that how to draw array of images using Graphics class in VB.net – C#.net.
To draw the array of images on form first of all make an array of class bitmap. After that we will draw an image under form paint event using graphics class.
C#
private Bitmap[] myImages = new Bitmap[3];
VB
Dim myImages() As Bitmap
Draw Image:
To draw the image we have a function of graphics class named drawimage (). This function takes 5 parameters. First is the object of image class. Then the x,y coordinates on the form where the image will draw. Then the height and width of the image. This function returns nothing. This function has 29 overloaded methods. You will call this function under the loop and will pass your image one by one in this function with different x,y coordinates of form. Array of images will be drawn on the form.
To demonstrate make a window application.
Now write the following code on Form Paint Event:
C#
private void Paint_Event(object sender, PaintEventArgs e)
{
try
{
Graphics g = e.Graphics;
int yOffset = 10;
int x = 10;
int height = 90;
int width = 90;
foreach (Bitmap b in myImages)
{
g.DrawImage(b, x, yOffset, height, width);
yOffset += 100;
}
}
catch (Exception ex) { }
}
VB
Private Sub Paint_Event(ByVal sender As Object, ByVal e As PaintEventArgs)
Try
Dim g As Graphics = e.Graphics
Dim yOffset As Integer = 10
Dim x As Integer = 10
Dim height As Integer = 90
Dim width As Integer = 90
For Each b As Bitmap In myImages
g.DrawImage(b, x, yOffset, height, width)
yOffset = (yOffset + 100)
Next
Catch ex As Exception
End Try
End Sub
This is simple code to draw the array of images on the form.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Devasp Application";
myImages[0] = new Bitmap("Jellyfish.jpg");
myImages[1] = new Bitmap("Koala.jpg");
myImages[2] = new Bitmap("Penguins.bmp");
}
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Me.Text = "Devasp Application"
myImages(0) = New Bitmap("Jellyfish.jpg")
myImages(1) = New Bitmap("Koala.jpg")
myImages(2) = New Bitmap("Penguins.bmp")
End Sub
This simple article tells that how to draw array of images using Graphics class in VB.net – C#.net.