Difference between revisions of "Drawing Graphics in Visual Basic"
(→Drawing a Ellipse) |
|||
Line 1: | Line 1: | ||
+ | <table border="0" cellspacing="0" width="100%"> | ||
+ | <tr> | ||
+ | <td width="20%">[[Working with Directories in Visual Basic|Previous]]<td align="center">[[Visual Basic Essentials|Table of Contents]]<td width="20%" align="right"></td> | ||
+ | <tr> | ||
+ | <td width="20%">Working with Directories in Visual Basic<td align="center"><td width="20%" align="right"></td> | ||
+ | </table> | ||
+ | <hr> | ||
+ | |||
+ | |||
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. | 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. | ||
Revision as of 19:08, 13 August 2007
Previous | Table of Contents | |
Working with Directories in Visual Basic |
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:
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:
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:
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:
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)
Drawing a Rectangle
Dim greenPen As New Drawing.Pen(Color.Green, 10) e.Graphics.DrawRectangle(greenPen, 10, 10, 100, 200)
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:
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)
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.