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
 

Draw a Bezier on form using Graphics class vb.net - C #.net.

Author: Maliha Atteeq
Download Source Code : 1022_graphicsApp.zip

In this simple article you will learn that how to draw a Bezier on the form using Graphics class in vb 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 a Bezier using system.drawing.graphics.

Bezier draw:

In order to draw a bezier 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)

Bezier draw function:

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

This function takes five arguments. The first argument is the object of Pen class. Pen class has the four overloaded constructor. You can use any of the constructors to initialize the object of pen class. I use the following:

C#:

System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black);

VB:

Dim pen As New System.Drawing.Pen(System.Drawing.Color.Black)

 

The remaining four arguments are of type point . The point class also provides us four overloaded constructor, depending on your requirement you can use any of them. I use the following:

C#:

Point point1 = new Point(25,35);

Vb:

Dim point1 As New System.Drawing.Point(25,35)

 

Pass all these arguments in the DrawArc() function provided by the graphics class. Once you do this then you have to call the Dispose() method to epose your created arc. 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#:

       pen.Dispose();

 

VB:

               pen.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.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black);

            System.Drawing.Graphics obj;

            obj = this.CreateGraphics();

            int x1 = 20;

            int y1 = 30;

            int x2 = 40;

            int y2 = 10;

            int x3 = 60;

            int y3 = 20;

            int x4 = 50;

            int y4 = 40;

            Point p1 = new Point(x1, y1);

            Point p2 = new Point(x2, y2);

            Point p3 = new Point(x3, y3);

            Point p4 = new Point(x4, y4);

            obj.DrawBezier(pen,p1,p2,p3,p4);

            pen.Dispose();

            obj.Dispose();

        }

VB:

Protected Overloads Overrides Sub OnPaint(ByVal e As PaintEventArgs)

    Dim pen As New System.Drawing.Pen(System.Drawing.Color.Black)

    Dim obj As System.Drawing.Graphics

    obj = Me.CreateGraphics()

    Dim x1 As Integer = 20

    Dim y1 As Integer = 30

    Dim x2 As Integer = 40

    Dim y2 As Integer = 10

    Dim x3 As Integer = 60

    Dim y3 As Integer = 20

    Dim x4 As Integer = 50

    Dim y4 As Integer = 40

    Dim p1 As New Point(x1, y1)

    Dim p2 As New Point(x2, y2)

    Dim p3 As New Point(x3, y3)

    Dim p4 As New Point(x4, y4)

    obj.DrawBezier(pen, p1, p2, p3, p4)

    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 Bezier DRAW APPLICATION";

           

        }

Vb:

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

 

        Me.Text = "DEVASP BEZIER DRAW APPLICATION"

    End Sub

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

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Draw an ellipse on form using Graphics class vb.net - C #.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net