Draw All Shapes : 2D Introduction « 2D Graphics « VB.Net Tutorial






Draw All Shapes
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

public class DrawAllKindsOf
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Sub New()
        MyBase.New()

        InitializeComponent()
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private components As System.ComponentModel.IContainer

    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents Button4 As System.Windows.Forms.Button
    Friend WithEvents Button5 As System.Windows.Forms.Button
    Friend WithEvents Button6 As System.Windows.Forms.Button
    Friend WithEvents Button7 As System.Windows.Forms.Button
    Friend WithEvents Button8 As System.Windows.Forms.Button
    Friend WithEvents Button9 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Button3 = New System.Windows.Forms.Button
        Me.Button4 = New System.Windows.Forms.Button
        Me.Button5 = New System.Windows.Forms.Button
        Me.Button6 = New System.Windows.Forms.Button
        Me.Button7 = New System.Windows.Forms.Button
        Me.Button8 = New System.Windows.Forms.Button
        Me.Button9 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.BackColor = System.Drawing.SystemColors.Window
        Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.Left
        Me.PictureBox1.Location = New System.Drawing.Point(0, 0)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(208, 266)
        Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(240, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(56, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Dot"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(240, 48)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(56, 24)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Line"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(240, 88)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(56, 24)
        Me.Button3.TabIndex = 3
        Me.Button3.Text = "Ellipse"
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(240, 128)
        Me.Button4.Name = "Button4"
        Me.Button4.Size = New System.Drawing.Size(56, 24)
        Me.Button4.TabIndex = 4
        Me.Button4.Text = "Arc"
        '
        'Button5
        '
        Me.Button5.Location = New System.Drawing.Point(240, 168)
        Me.Button5.Name = "Button5"
        Me.Button5.Size = New System.Drawing.Size(56, 24)
        Me.Button5.TabIndex = 5
        Me.Button5.Text = "Pie"
        '
        'Button6
        '
        Me.Button6.Location = New System.Drawing.Point(240, 208)
        Me.Button6.Name = "Button6"
        Me.Button6.Size = New System.Drawing.Size(56, 24)
        Me.Button6.TabIndex = 6
        Me.Button6.Text = "Rectangle"
        '
        'Button7
        '
        Me.Button7.Location = New System.Drawing.Point(312, 8)
        Me.Button7.Name = "Button7"
        Me.Button7.Size = New System.Drawing.Size(56, 24)
        Me.Button7.TabIndex = 7
        Me.Button7.Text = "Triangle"
        '
        'Button8
        '
        Me.Button8.Location = New System.Drawing.Point(312, 48)
        Me.Button8.Name = "Button8"
        Me.Button8.Size = New System.Drawing.Size(56, 24)
        Me.Button8.TabIndex = 8
        Me.Button8.Text = "Polygon"
        '
        'Button9
        '
        Me.Button9.Location = New System.Drawing.Point(312, 88)
        Me.Button9.Name = "Button9"
        Me.Button9.Size = New System.Drawing.Size(56, 24)
        Me.Button9.TabIndex = 9
        Me.Button9.Text = "Bezier Curve"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(376, 246)
        Me.Controls.Add(Me.Button9)
        Me.Controls.Add(Me.Button8)
        Me.Controls.Add(Me.Button7)
        Me.Controls.Add(Me.Button6)
        Me.Controls.Add(Me.Button5)
        Me.Controls.Add(Me.Button4)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.PictureBox1)
        Me.ResumeLayout(False)

    End Sub

    Dim g As System.Drawing.Graphics
    Dim pen1 As New System.Drawing.Pen(Color.Red, 3)
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Refresh()
        g = PictureBox1.CreateGraphics
        Dim point1 As System.Drawing.Point
        Dim point2 As System.Drawing.Point
        point1.x = 10 
        point1.y = 20 

        point2.X = 11 
        point2.Y = 21
        g.DrawLine(pen1, point1, point2)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        g = PictureBox1.CreateGraphics
        g.DrawLine(pen1, 20, 20, 160, 160)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        g = PictureBox1.CreateGraphics
        g.DrawEllipse(pen1, 50, 50, 100, 150)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        g = PictureBox1.CreateGraphics
        g.DrawArc(pen1, 90, 50, 100, 150, 150, 160)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        g = PictureBox1.CreateGraphics
        g.DrawPie(pen1, 50, 50, 150, 150, 0, 170)
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        g = PictureBox1.CreateGraphics
        g.DrawRectangle(pen1, 30, 30, 50, 60)
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim p(3) As System.Drawing.Point
        p(0).X = 0
        p(0).Y = 0
        p(1).X = 40
        p(1).Y = 120
        p(2).X = 110
        p(2).Y = 70
        g = PictureBox1.CreateGraphics
        g.DrawPolygon(pen1, p)
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Dim p(5) As System.Drawing.Point
        p(0).X = 0
        p(0).Y = 0
        p(1).X = 40
        p(1).Y = 120
        p(2).X = 110
        p(2).Y = 70
        p(3).X = 30
        p(3).Y = 30
        p(4).X = 170
        p(4).Y = 10
        g = PictureBox1.CreateGraphics
        g.DrawPolygon(pen1, p)
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        g = PictureBox1.CreateGraphics
        g.DrawBezier(pen1, 10, 20, 140, 150, 100, 30, 150, 200)
    End Sub
End Class








17.1.2D Introduction
17.1.1.Draw All ShapesDraw All Shapes