Changes

Jump to: navigation, search

Drawing Graphics in Visual Basic

3,774 bytes added, 20:01, 10 August 2007
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 ...
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:

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

<pre>
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
</pre>

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:

<pre>
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
</pre>

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:

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

<pre>
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)
</pre>

The above code produces the following output:

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

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

e.Graphics.DrawLine(greenPen, 20, 30, 100, 100)
</pre>

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

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

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

e.Graphics.DrawEllipse(greenPen, 10, 10, 100, 200)
</pre>

[[Image:vb_draw_ellipse.jpg]]

=== Drawing a Rectangle ==

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

e.Graphics.DrawRectangle(greenPen, 10, 10, 100, 200)
</pre>

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

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

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:

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

e.Graphics.DrawString("Techotopia Rocks", fontObj, Brushes.Chocolate, 10, 10)
</pre>

The above code draws the following text:

[[Image:vb_draw_text.jpg]]

Navigation menu