Drawing Graphics in Visual Basic

Follow Techotopia on Twitter Bookmark and Share

From Techotopia

Jump to: navigation, search
PreviousTable of Contents
Working with Directories in Visual Basic


Purchase and download the fully updated Visual Basic 2010 version of this eBook in PDF and ePub for only $9.99  Add to Cart


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

Start Visual Studio and create a new Windows Application project.

Contents



[edit] Drawing 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 pull-down list, select the Paint event as shown below (if you are using Visual Studio 2008 the events are accessed by clicking on the lightning bolt above the properties list):

Image:vb_graphics_paint_event.jpg

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

    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 Graphics 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:

Image: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:

Image:vb_brush_gradient.jpg

[edit] 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:

Image:vb_pen_drawline.jpg

[edit] Drawing Shapes in Visual Basic

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

[edit] Drawing a Ellipse

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

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

Image:vb_pen_ellipse.jpg

[edit] Drawing a Rectangle

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

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

Image:vb_draw_rectange.jpg

[edit] 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:

Image:vb_draw_text.jpg

[edit] Clearing a Drawing Area

A drawing area may be cleared using any color via the Visual Basic Graphics object Clear() method as follows:

e.Graphics.Clear(Color.White)

[edit] Summary

This chapter is intended to provide a basic grounding in Graphics drawing in Visual Basic. The area of graphics drawing is vast and it is impossible to cover everything in a single chapter. Hopefully enough has been covered hear to give you the confidence to experiment and learn more.


Purchase and download the fully updated Visual Basic 2010 edition of this eBook in PDF and ePub for only $9.99

Add to Cart

Views
Personal tools

Find us on Facebook