Drawing Graphics in Visual Basic

Revision as of 20:01, 10 August 2007 by Neil (Talk | contribs) (New page: In the chapter of Visual Basic Essentials we will cover the drawing of 2D graphics on controls using Visual Basic. In the tutorial we will create a project containing a blank form and ...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Revision as of 20:01, 10 August 2007 by Neil (Talk | contribs) (New page: In the chapter of Visual Basic Essentials we will cover the drawing of 2D graphics on controls using Visual Basic. In the tutorial we will create a project containing a blank form and ...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In the chapter of Visual Basic Essentials we will cover the drawing of 2D graphics on controls using Visual Basic. In the tutorial we will create a project containing a blank form and wrok step by step through drawing on the form.

Start Visual Studio and create a new Windows Application project.

Drawing a Filled Shapes in Visual Basic

Begin by double clicking on the new Form in the Visual Studio project to access the event procedure code for the form control. From the pulldown list, select the Paint event as shown below:

Vb graphics paint event.jpg

Once selected Visual Studio will display the Paint event subroutine. The next task is to create a paintbrush to paint with:

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) 
          Handles Me.Paint

        Dim blueBrush As New Drawing.SolidBrush(Color.Blue)

    End Sub

In the above code we have created a new SolidBrush drawing object using color blue and assigned it to variable blueBrush. The next step is to use the brush to draw a shape:

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) 
                  Handles Me.Paint
        Dim blueBrush As New Drawing.SolidBrush(Color.Blue)

        e.Graphics.FillRectangle(blueBrush, 20, 30, 100, 100)
    End Sub

The above example tells the Graphic object to draw a rectangle at coordinates x=20, y=30 of height and width equal to 100. Press 'F5 to build and run the application which should appear as follows:

Vb brush rectangle.jpg

Visual Basic provides a wide range of brush effects. A gradient effect, for example, can be achieved using the LinearGradientBrush:

        Dim myRectangle As New Drawing.Rectangle(10, 10, 100, 100)

        Dim myGradient As New Drawing2D.LinearGradientBrush(myRectangle, Color.Red, Color.Blue, 50)
        e.Graphics.FillRectangle(myGradient, 20, 30, 100, 100)

The above code produces the following output:

Vb brush gradient.jpg

Drawing in Visual Basic Using a Pen

The Visual Basic Pen object can be used to draw lines between specific points and of specific widths as follows:

Dim greenPen As New Drawing.Pen(Color.Green, 10)

e.Graphics.DrawLine(greenPen, 20, 30, 100, 100)

The above example draws a line between the coordinates provided using a width of 10 pixels as follows:

Vb pen drawline.jpg


Drawing Shapes in Visual Basic

The following code excerpts demonstrate how to draw various shapes in Visual Basic:

Drawing a Ellipse

        Dim greenPen As New Drawing.Pen(Color.Green, 10)

        e.Graphics.DrawEllipse(greenPen, 10, 10, 100, 200)

File:Vb draw ellipse.jpg

= Drawing a Rectangle

        Dim greenPen As New Drawing.Pen(Color.Green, 10)

        e.Graphics.DrawRectangle(greenPen, 10, 10, 100, 200)

Vb draw rectange.jpg

Drawing Text in Visual Basic

Text is drawn in Visual Basic using the DrawString() method. This takes the text, font object, brush and coordinates as parameters.

First a font object needs to be defined. This involves specifying the font type and size:

Dim fontObj As Font
fontObj = new System.Drawing.Font("Times", 25, FontStyle.Bold)

A number of brush types are preconfigured for your use in Visual Basic. In this example we will use the System.Drawing.Brushes.Chocolate brush:

        Dim fontObj As Font
        fontObj = New System.Drawing.Font("Times", 25, FontStyle.Bold)

        e.Graphics.DrawString("Techotopia Rocks", fontObj, Brushes.Chocolate, 10, 10)

The above code draws the following text:

Vb draw text.jpg