Saturday, October 22, 2011

Drawing Lines, Shapes and Text in VB. NET

.NET has inbuilt methods for drawing graphics for your program. In order to use them you will need to instantiate a Graphics object. This object can be used do draw shapes and lines anywhere in your form, but in most cases you will prefer to use a PictureBox control as your "drawing pad". Also, in order to initialize your graphics, you will need to use a Bitmap object.

After creating your form and adding the PictureBox, you need to create an OnLoad event (which will trigger when the form is created) and add the following instructions:
    'The Form is called Form1 and the PictureBox is called PictureBox1
    Private myGraphics As Graphics
    Private myBitmap As Bitmap

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myBitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        myGraphics = Graphics.FromImage(myBitmap)
    End Sub
Drawing a Line
To draw a line you need to specify a pen and two points that belong to that line. You can specify the points using the Point/PointF class or by providing the coordinates as Integers/Singles. After you made a graphical change, you will need to update the PictureBox's image (this is a most do for all graphical changes).
Example:
        myGraphics.DrawLine(New Pen(Color.Black),
                            New Point(10, 20),
                            New Point(15, 30))
        PictureBox1.Image = myBitmap
Drawing a Rectangle
To draw a rectangle you will need to specify a Pen, the coordinates of the upper-leftmost vertex, the width and the height of your Rectangle.
Example:
        myGraphics.DrawRectangle(New Pen(Color.Black), 10, 20, 30, 50)
        PictureBox1.Image = myBitmap
Drawing an Arc
To draw an arc you will need to specify a Pen, a Rectangle area to contain the arc, the start angle and the end angle (which are specified in degrees))
Example:
        myGraphics.DrawArc(New Pen(Color.Black),
                           New Rectangle(10, 20, 30, 50), 23.5, 90.5)
        PictureBox1.Image = myBitmap
Drawing a Bezier Spline
To draw a Bezier spline you will need to specify a Pen, a starting point, two control points and an ending point. The points can be specified either by using the Point/PointF class or by inputting the coordinates for each point specifically as Integers/Singles.
Example:
        myGraphics.DrawBezier(New Pen(Color.Black),
                              New Point(20, 30), New Point(50, 80),
                              New Point(12, 30), New Point(40, 30))
Drawing an Ellipse 
To draw an ellipse you will need to specify a Pen and a rectangle area which will contain your ellipse.
Example:
       myGraphics.DrawEllipse(New Pen(Color.Black), 
       New Rectangle(100, 100, 100, 100))
       PictureBox1.Image = myBitmap
Drawing a Pie Slice
To draw a pie slice (a section from an ellipse) you will need to specify a Pen, a rectangle area to contain the pie (the entire pie), the start angle and the end angle of the section (in degrees).
Example:
       myGraphics.DrawPie(New Pen(Color.Red), 
       New Rectangle(100, 100, 100, 100), 
       -32.0, -48)
       PictureBox1.Image = myBitmap
Drawing a Polygon
To draw a polygon you will need to specify a Pen and a point vector that symbolize the polygon's vertices.
Example:
        Dim pointArray(0 To 3) As Point
        pointArray(0) = New Point(20, 30)
        pointArray(1) = New Point(40, 50)
        pointArray(2) = New Point(80, 90)
        pointArray(3) = New Point(40, 80)
        myGraphics.DrawPolygon(New Pen(Color.BlueViolet), pointArray)
Drawing Curves/Shapes
To draw a curve/shape you will need to specify a Pen and a point vector who will contain a number of points who belong to the curve. The method is based on an interpolation algorithm which tries to approximate the missing points coordinates that stand between the points you specify (the accuracy of the drawing depends on how many points you offer).
Example:
        Dim pointArray(0 To 3) As Point
        pointArray(0) = New Point(20, 30)
        pointArray(1) = New Point(40, 50)
        pointArray(2) = New Point(80, 90)
        pointArray(3) = New Point(40, 80)
        'Drawing a closed curve(shape)
        myGraphics.DrawClosedCurve(New Pen(Color.Blue), pointArray)
        'Drawing an open curve(does not interpolate the first and last point)
        myGraphics.DrawCurve(New Pen(Color.Red), pointArray)
        PictureBox1.Image = myBitmap
Drawing Text
To draw text you will need to specify the text which will be written as a String, the Font which will be used to render the text, a Brush and the point at which the drawing will start.
Example
       myGraphics.DrawString("TEXT TO BE WRITTEN", 
                             New Font("Times New Roman", 13),
                             New SolidBrush(Color.Red), New Point(80, 80))
       PictureBox1.Image = myBitmap

1 comment:

  1. how to draw line using maouse in picturebox ad display the drawing line

    ReplyDelete

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger