Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to draw an image on the form using Graphics class in vb.net - C #.net.

Author: Maliha Atteeq
Download Source Code : 1046_apps.zip

In this simple article you will learn that how to draw an image on the form using Graphics class in vb.net and C #.net.

 Graphics:

This is the very important class for making graphics of your own using c#. This class cannot be inherited. This class provides us a number of different methods for graphics creation. By using these methods you can create graphics of your own choice.

In this article I will tell you that how can we draw an image using system.drawing.graphics.

Image draw:

In order to draw an image on the form, firstly you should know the behavior of the form. While playing with the graphics you must keep the following point in your mind:

You can’t draw graphics on simple load event of the form. To create graphics on the form, you must override the onpaint() function of the form.

The syntax of the onpaint() function is:

C#:

protected override void OnPaint (PaintEventArgs e)

{

}

VB:

Dim e As PaintEventArgs

 

Me.OnPaint(e)

Image draws function:

In order to draw an image, graphics class provides us a function named as drawImage(). It is an overloaded function. By using this function you can draw an image on the form. Depending on your requirement you can use any of the overloaded function. I use the following:

This function takes two arguments. The first argument is the object of point class. It has four overloaded functions. Depending on your requirements, you can choose anyone.

C#:

System.Drawing.Point point = new Point(x, y);

VB:

        Dim point As System.Drawing.Point = New Point(x, y)

The second argument is an object of image class. This class provides us some static functions that gives you the option to load the image from different ways. Depending on your requirement you can use any of them. I use the following constructor.

 C#:               

System.Drawing.Image.FromFile("d:\\test.jpg");

VB:

System.Drawing.Image.FromFile("d:\test.jpg")

Pass all these arguments in the Drawimage() function provided by the graphics class. Once you do this then you have to call the Dispose() method to epose your created closeCurve. The dispose() function takes no argument as a paameter. This function simply expose your graphics on the form. You have to call this function with the object of graphics and also with the object of pen class.

 

C#:

       obj.Dispose();

 

VB:

               obj.Dispose();

 

To demonstrate make a new window application and write the following code on form’s onpaint event:

C#:

protected override void OnPaint(PaintEventArgs e)

        {

            System.Drawing.Graphics obj;

            obj = this.CreateGraphics();

            int x = 70;

            int y = 80;

            System.Drawing.Point point = new Point(x, y);

            obj.Clear(Color.White);

            obj.DrawImage(System.Drawing.Image.FromFile("d:\\test.jpg"), point);

            pen.Dispose();

            obj.Dispose();

        }

VB:     

Protected Overloads Overrides Sub OnPaint(ByVal e As PaintEventArgs)

        Dim obj As System.Drawing.Graphics

        obj = Me.CreateGraphics()

        Dim x As Integer = 70

        Dim y As Integer = 80

        Dim point As System.Drawing.Point = New Point(x, y)

        obj.Clear(Color.White)

        obj.DrawImage(System.Drawing.Image.FromFile("d:\test.jpg"), point)

        pen.Dispose()

        obj.Dispose()

    End Sub

Now write the following code on FORM LOAD event:

C#:

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP IMAGE APPLICATION";

           

        }

Vb:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

 

        Me.Text = "DEVASP IMAGE APPLICATION"

    End Sub

This simple article tells that how to draw an image using Graphics class in vb and C #.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to draw a clipped image on form using Graphics class in vb.net - C #.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net