Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

-Creating Graphics

Introduction

• Before you can draw anything on a form, you need to create the
Graphics object in Visual Basic .
• A graphics object is created using the CreateGraphics() method.
• You can create a graphics object that draws to the form itself or a
control.
Example
Creating a Pen
Drawing a Line

• In this section, we will show you how to draw a straight line on the
Form.
• First of all, launch Visual basic. In the start up page, drag a button into
the form.
• Double click on the button and key in the next code.
Drawing a Line
Drawing a Line

Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click


Dim myGraphics As Graphics = Me.CreateGraphics
Dim myPen As Pen
myPen = New Pen(Brushes.DarkMagenta, 20)
myGraphics.DrawLine(myPen, 60, 180, 220, 50)
End Sub
Drawing a Rectangle

• Method 1
• The first is to draw a rectangle directly using
the DrawRectangle method by specifying its upper-left corner’s
coordinate and its width and height. The method of the Graphics
object to draw the rectangle is DrawRectangle.The syntax is:.
myGrapphics.DrawRectangle(myPen, X, Y, width, height)
Drawing a Rectangle
Drawing a Rectangle
Drawing a Rectangle

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim myPen As Pen myPen = New Pen(Drawing.Color.Blue, 5)

Dim myGraphics As Graphics = Me.CreateGraphics

myPen.DashStyle = Drawing.Drawing2D.DashStyle.Dot

myGraphics.DrawRectangle(myPen, 10, 10, 100, 50)

End Sub
Drawing a Rectangle
Drawing a
Rectangle
Drawing a
Rectangle
Drawing Text
Drawing Text
Drawing Text

You might also like