VB.Net Tutorial/2D Graphics/Pen

Материал из VB Эксперт
Перейти к: навигация, поиск

Adjustable RowCap

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class AdjustableRowCap

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

End class public class Form1

 Inherits System.Windows.Forms.Form
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       Dim g As Graphics = Me.CreateGraphics()
       g.Clear(Me.BackColor)
       Dim cap1 As New AdjustableArrowCap(1, 1, False)
       Dim cap2 As New AdjustableArrowCap(2, 1)
       cap1.BaseCap = LineCap.Round
       cap1.BaseInset = 5
       cap1.StrokeJoin = LineJoin.Bevel
       cap2.WidthScale = 3
       cap2.BaseCap = LineCap.Square
       cap2.Height = 1
       Dim blackPen As New Pen(Color.Black, 15)
       blackPen.CustomStartCap = cap1
       blackPen.CustomEndCap = cap2
       g.DrawLine(blackPen, 20, 50, 200, 50)
       blackPen.Dispose()
       g.Dispose()
 End Sub
 Public Sub New()
  
   MyBase.New()
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(292, 273)
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
 End Sub

End Class</source>

Create Pen from Color

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class GDIObjectsPenColor

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

End class Public Class Form1

   Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
       Dim canvas As Graphics = e.Graphics
       
       Dim pen1 As New Pen(Color.Blue)
       canvas.DrawEllipse(pen1, 100, 50, 300, 200)
       
       canvas = Nothing
   End Sub

End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing AndAlso components IsNot Nothing Then
           components.Dispose()
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.SuspendLayout()
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(610, 328)
       Me.Name = "Form1"
       Me.Text = "Creating Graphics Objects (Color, Pen, Font, Brush)"
       Me.ResumeLayout(False)
   End Sub

End Class</source>

Create Pen from HatchBrush

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class CreatePenFromHatchBrush

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

End class public class Form1

 Inherits System.Windows.Forms.Form
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       Dim g As Graphics = Me.CreateGraphics()
       g.Clear(Me.BackColor)
       " Create a solid and hatch brush
       Dim blueBrush As New SolidBrush(Color.Blue)
       Dim hatchBrush As New HatchBrush(HatchStyle.DashedVertical, Color.Black, Color.Green)
       " Create a pen from a solid brush with
       " width 3
       Dim pn1 As New Pen(blueBrush, 3)
       " Create a pen from a hatch brush
       Dim pn2 As New Pen(hatchBrush, 8)
       " Create a pen from a Color structure
       Dim pn3 As New Pen(Color.Red)
       " Draw a line, ellipse, and rectangle
       g.DrawLine(pn1, New Point(10, 40), New Point(10, 90))
       g.DrawEllipse(pn2, 20, 50, 100, 100)
       g.DrawRectangle(pn3, 40, 90, 100, 100)
       " Dispose
       pn1.Dispose()
       pn2.Dispose()
       pn3.Dispose()
       blueBrush.Dispose()
       hatchBrush.Dispose()
       g.Dispose()
 End Sub
 Public Sub New()
  
   MyBase.New()
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(292, 273)
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
 End Sub

End Class</source>

Create Pen from LinearGradientBrush

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class PenStyleLinearGradientBrush

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

End class public class Form1

 Inherits System.Windows.Forms.Form
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       Dim g As Graphics = Me.CreateGraphics()
       g.Clear(Me.BackColor)
       Dim img = New Bitmap("yourfile.jpg")
       Dim redBrush As New SolidBrush(Color.Red)
       Dim txtrBrush As New TextureBrush(img)
       Dim lgBrush As New LinearGradientBrush(New Rectangle(10, 10, 10, 10), Color.Red, Color.Black, 45.0F)
       Dim pn1 As New Pen(redBrush, 4)
       Dim pn2 As New Pen(txtrBrush, 20)
       Dim pn3 As New Pen(lgBrush, 20)
     
       g.DrawEllipse(pn1, 100, 100, 50, 50)
       g.DrawRectangle(pn2, 80, 80, 100, 100)
       g.DrawEllipse(pn3, 30, 30, 200, 200)
       Console.WriteLine(pn1.PenType.ToString())
       Console.WriteLine(pn2.PenType.ToString())
       Console.WriteLine(pn3.PenType.ToString())
       redBrush.Dispose()
       txtrBrush.Dispose()
       lgBrush.Dispose()
       img.Dispose()
       pn1.Dispose()
       pn2.Dispose()
       pn3.Dispose()
       g.Dispose()
 End Sub
 Public Sub New()
  
   MyBase.New()
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(292, 273)
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
 End Sub

End Class</source>

Create Pen from SolidBrush

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class PenStyleSolidBrush

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

End class public class Form1

 Inherits System.Windows.Forms.Form
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       " Create a Graphics object
       Dim g As Graphics = Me.CreateGraphics()
       g.Clear(Me.BackColor)
       " Create three different types of brushes
       Dim img = New Bitmap("yourfile.jpg")
       Dim redBrush As New SolidBrush(Color.Red)
       Dim txtrBrush As New TextureBrush(img)
       Dim lgBrush As New LinearGradientBrush(New Rectangle(10, 10, 10, 10), Color.Red, Color.Black, 45.0F)
       Dim pn1 As New Pen(redBrush, 4)
       Dim pn2 As New Pen(txtrBrush, 20)
       Dim pn3 As New Pen(lgBrush, 20)
     
       g.DrawEllipse(pn1, 100, 100, 50, 50)
       g.DrawRectangle(pn2, 80, 80, 100, 100)
       g.DrawEllipse(pn3, 30, 30, 200, 200)
       Console.WriteLine(pn1.PenType.ToString())
       Console.WriteLine(pn2.PenType.ToString())
       Console.WriteLine(pn3.PenType.ToString())
       redBrush.Dispose()
       txtrBrush.Dispose()
       lgBrush.Dispose()
       img.Dispose()
       pn1.Dispose()
       pn2.Dispose()
       pn3.Dispose()
       g.Dispose()
 End Sub
 Public Sub New()
  
   MyBase.New()
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(292, 273)
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
 End Sub

End Class</source>

Create Pen from TextureBrush

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class PenStyleTextureBrush

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

End class public class Form1

 Inherits System.Windows.Forms.Form
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       Dim g As Graphics = Me.CreateGraphics()
       g.Clear(Me.BackColor)
       Dim img = New Bitmap("yourfile.jpg")
       Dim redBrush As New SolidBrush(Color.Red)
       Dim txtrBrush As New TextureBrush(img)
       Dim lgBrush As New LinearGradientBrush(New Rectangle(10, 10, 10, 10), Color.Red, Color.Black, 45.0F)
       Dim pn1 As New Pen(redBrush, 4)
       Dim pn2 As New Pen(txtrBrush, 20)
       Dim pn3 As New Pen(lgBrush, 20)
     
       g.DrawEllipse(pn1, 100, 100, 50, 50)
       g.DrawRectangle(pn2, 80, 80, 100, 100)
       g.DrawEllipse(pn3, 30, 30, 200, 200)
       Console.WriteLine(pn1.PenType.ToString())
       Console.WriteLine(pn2.PenType.ToString())
       Console.WriteLine(pn3.PenType.ToString())
       redBrush.Dispose()
       txtrBrush.Dispose()
       lgBrush.Dispose()
       img.Dispose()
       pn1.Dispose()
       pn2.Dispose()
       pn3.Dispose()
       g.Dispose()
 End Sub
 Public Sub New()
  
   MyBase.New()
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(292, 273)
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
 End Sub

End Class</source>

Create Pen with Color and Width

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class GDIObjectsCreatePenWidth

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

End class Public Class Form1

   Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
       Dim canvas As Graphics = e.Graphics
       
       Dim pen1 As New Pen(Color.Blue, 30)
       canvas.DrawEllipse(pen1, 100, 50, 300, 200)
       
       canvas = Nothing
   End Sub

End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing AndAlso components IsNot Nothing Then
           components.Dispose()
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.SuspendLayout()
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(610, 328)
       Me.Name = "Form1"
       Me.Text = "Creating Graphics Objects (Color, Pen, Font, Brush)"
       Me.ResumeLayout(False)
   End Sub

End Class</source>

Draw thick rectangle outline in red

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ThickPenDemo

  public Shared Sub Main
       Application.Run(New FrmDrawShapes)
  End Sub

End class Public Class FrmDrawShapes

  Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
  Public Sub New()
     MyBase.New()
     "This call is required by the Windows Form Designer.
     InitializeComponent()
     "Add any initialization after the InitializeComponent() call
  End Sub
  "Form overrides dispose to clean up the component list.
  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
  "Required by the Windows Form Designer
  Private components As System.ruponentModel.Container
  "NOTE: The following procedure is required by the Windows Form Designer
  "It can be modified using the Windows Form Designer.  
  "Do not modify it using the code editor.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
     "
     "frmDrawShapes
     "
     Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
     Me.ClientSize = New System.Drawing.Size(520, 197)
     Me.Name = "frmDrawShapes"
     Me.Text = "Drawing Shapes"
  End Sub
  1. End Region
  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
     Dim graphicsObject As Graphics = e.Graphics
     Dim thickRedPen As Pen = New Pen(Color.Red, 10)
     Dim drawArea2 As Rectangle = New Rectangle(80, 30, 65, 100)
     
     graphicsObject.DrawRectangle(thickRedPen, drawArea2)
  End Sub 

End Class</source>

One pixel pen

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class PenOnePixel

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

End class Public Class Form1

   Private Sub Form11_Paint(ByVal sender As Object, _
         ByVal e As System.Windows.Forms.PaintEventArgs) _
         Handles Me.Paint
        e.Graphics.PageUnit = GraphicsUnit.Millimeter
       e.Graphics.DrawLine(New Pen(Color.Black, 1 / e.Graphics.DpiX), 0, 0, 200, 200)
   End Sub

End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing AndAlso components IsNot Nothing Then
           components.Dispose()
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.SuspendLayout()
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(617, 197)
       Me.MaximizeBox = False
       Me.ResumeLayout(False)
   End Sub

End Class</source>

PenAlignment.Inset, PenAlignment.Outset, PenAlignment.Left

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class PenAlignmentInsetOutsetLeft

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

End class public class Form1

 Inherits System.Windows.Forms.Form
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       Dim g As Graphics = Me.CreateGraphics()
       g.Clear(Me.BackColor)
       Dim pn1 As New Pen(Color.Red, 3)
       Dim pn2 As New Pen(Color.Blue)
       Dim pn3 As New Pen(Color.Red)
       Dim greenBrush As New SolidBrush(Color.Green)
       pn1.Color = Color.Black
       pn1.Brush = greenBrush
       pn1.Width = 4
       pn1.Alignment = PenAlignment.Left
       Dim style As PenType = pn1.PenType
       pn2.Width = 3
       pn2.Alignment = PenAlignment.Inset
       pn3.Width = 3
       pn3.Alignment = PenAlignment.Outset
       g.DrawLine(pn1, New Point(10, 10), New Point(150, 10))
       g.DrawLine(pn2, New Point(10, 30), New Point(200, 30))
       g.DrawLine(pn3, New Point(10, 50), New Point(250, 50))
       pn1.Dispose()
       pn2.Dispose()
       pn3.Dispose()
       g.Dispose()
 End Sub
 Public Sub New()
   MyBase.New()
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(292, 273)
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
 End Sub

End Class</source>

Pen.ScaleTransform

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ScaleTransformPen

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

End class public class Form1

 Inherits System.Windows.Forms.Form
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       Dim g As Graphics = Me.CreateGraphics()
       g.Clear(Me.BackColor)
       Dim bluePen As New Pen(Color.Blue, 10)
       Dim redPen As New Pen(Color.Red, 5)
       bluePen.ScaleTransform(3, 1)
       g.DrawEllipse(bluePen, 20, 20, 100, 50)
       g.DrawRectangle(redPen, 20, 120, 100, 50)
       bluePen.RotateTransform(90, MatrixOrder.Append)
       redPen.ScaleTransform(4, 2, MatrixOrder.Append)
       g.DrawEllipse(bluePen, 220, 20, 100, 50)
       g.DrawRectangle(redPen, 220, 120, 100, 50)
       redPen.Dispose()
       bluePen.Dispose()
       g.Dispose()
 End Sub
 Public Sub New()
  
   MyBase.New()
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(292, 273)
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
 End Sub

End Class</source>

Pen.SetLineCap

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class PenSetLineCap

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

End class public class Form1

 Inherits System.Windows.Forms.Form
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       Dim g As Graphics = Me.CreateGraphics()
       g.Clear(Me.BackColor)
       Dim redPen As New Pen(Color.Red, 6)
       Dim bluePen As New Pen(Color.Blue, 7)
       Dim greenPen As New Pen(Color.Green, 7)
       redPen.Width = 8
       redPen.DashStyle = DashStyle.Dash
       redPen.SetLineCap(LineCap.DiamondAnchor, LineCap.ArrowAnchor, DashCap.Flat)
       greenPen.DashStyle = DashStyle.DashDotDot
       greenPen.StartCap = LineCap.Triangle
       greenPen.EndCap = LineCap.Triangle
       greenPen.DashCap = DashCap.Triangle
       greenPen.DashStyle = DashStyle.Dot
       greenPen.DashOffset = 3.4F
       bluePen.StartCap = LineCap.Square
       bluePen.EndCap = LineCap.SquareAnchor
       greenPen.SetLineCap(LineCap.RoundAnchor, LineCap.Square, DashCap.Round)
       g.DrawLine(redPen, New Point(20, 50), New Point(150, 50))
       g.DrawLine(greenPen, New Point(30, 80), New Point(200, 80))
       g.DrawLine(bluePen, New Point(30, 120), New Point(250, 120))
       redPen.Dispose()
       greenPen.Dispose()
       greenPen.Dispose()
       g.Dispose()
 End Sub
 Public Sub New()
  
   MyBase.New()
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(292, 273)
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
 End Sub

End Class</source>

Pen Style: Dot, Dot Dot, Dot Dash, Dash Dot

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class PenStyle

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

End class 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.ruponentModel.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
   <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.SuspendLayout()
       "
       "PictureBox1
       "
       Me.PictureBox1.BackColor = System.Drawing.SystemColors.Window
       Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
       Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.Top
       Me.PictureBox1.Location = New System.Drawing.Point(0, 0)
       Me.PictureBox1.Name = "PictureBox1"
       Me.PictureBox1.Size = New System.Drawing.Size(360, 176)
       Me.PictureBox1.TabIndex = 0
       Me.PictureBox1.TabStop = False
       "
       "Button1
       "
       Me.Button1.Location = New System.Drawing.Point(8, 192)
       Me.Button1.Name = "Button1"
       Me.Button1.Size = New System.Drawing.Size(72, 32)
       Me.Button1.TabIndex = 1
       Me.Button1.Text = "Dashed Line"
       "
       "Button2
       "
       Me.Button2.Location = New System.Drawing.Point(96, 192)
       Me.Button2.Name = "Button2"
       Me.Button2.Size = New System.Drawing.Size(72, 32)
       Me.Button2.TabIndex = 2
       Me.Button2.Text = "Dotted Line"
       "
       "Button3
       "
       Me.Button3.Location = New System.Drawing.Point(184, 192)
       Me.Button3.Name = "Button3"
       Me.Button3.Size = New System.Drawing.Size(80, 32)
       Me.Button3.TabIndex = 3
       Me.Button3.Text = "Dot Dot"
       "
       "Button4
       "
       Me.Button4.Location = New System.Drawing.Point(280, 192)
       Me.Button4.Name = "Button4"
       Me.Button4.Size = New System.Drawing.Size(72, 32)
       Me.Button4.TabIndex = 4
       Me.Button4.Text = "Dot"
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(360, 238)
       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 pen1 As System.Drawing.Pen
   Dim g As System.Drawing.Graphics
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       PictureBox1.Refresh()
       g = PictureBox1.CreateGraphics
       pen1 = New System.Drawing.Pen(Color.Green, 3)
       pen1.DashStyle = Drawing.Drawing2D.DashStyle.Dash
       g.DrawEllipse(pen1, 70, 10, 100, 150)
       g.DrawLine(pen1, 10, 10, 120, 30)
   End Sub
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       PictureBox1.Refresh()
       g = PictureBox1.CreateGraphics
       pen1 = New System.Drawing.Pen(Color.Green, 3)
       pen1.DashStyle = Drawing.Drawing2D.DashStyle.DashDot
       g.DrawEllipse(pen1, 70, 10, 100, 150)
       g.DrawLine(pen1, 10, 10, 120, 30)
   End Sub
   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
       PictureBox1.Refresh()
       g = PictureBox1.CreateGraphics
       pen1 = New System.Drawing.Pen(Color.Green, 3)
       pen1.DashStyle = Drawing.Drawing2D.DashStyle.DashDotDot
       g.DrawEllipse(pen1, 70, 10, 100, 150)
       g.DrawLine(pen1, 10, 10, 120, 30)
   End Sub
   Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
       PictureBox1.Refresh()
       g = PictureBox1.CreateGraphics
       pen1 = New System.Drawing.Pen(Color.Green, 3)
       pen1.DashStyle = Drawing.Drawing2D.DashStyle.Dot
       g.DrawEllipse(pen1, 70, 10, 100, 150)
       g.DrawLine(pen1, 10, 10, 120, 30)
   End Sub

End Class</source>

PixelOffsetMode

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Imaging Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class PixelOffsetMode

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

End class public class Form1

 Inherits System.Windows.Forms.Form
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       Dim g As Graphics = Me.CreateGraphics()
       g.Clear(Me.BackColor)
       Dim redPen As New Pen(Color.Red, 6)
       Dim bluePen As New Pen(Color.Blue, 10)
       Dim blackPen As New Pen(Color.Black, 5)
       g.PixelOffsetMode = PixelOffsetMode.Half
       g.DrawRectangle(bluePen, 10, 20, 100, 50)
       g.DrawEllipse(redPen, 10, 150, 100, 50)
       g.DrawLine(blackPen, 150, 100, 250, 220)
       g.Dispose()
 End Sub
 Public Sub New()
  
   MyBase.New()
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(292, 273)
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
 End Sub

End Class</source>

Set Line StrokeCaps

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class SetStrokeCaps

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

End class public class Form1

 Inherits System.Windows.Forms.Form
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       " Create a graphic 
       Dim g As Graphics = Me.CreateGraphics()
       g.Clear(Me.BackColor)
       Dim points As Point() = {New Point(-3, -3), New Point(0, 0), New Point(3, -3)}
       Dim path As New GraphicsPath
       path.AddLines(points)
       Dim cap As New CustomLineCap(Nothing, path)
       cap.SetStrokeCaps(LineCap.Round, LineCap.Triangle)
       Dim redPen As New Pen(Color.Red, 15)
       redPen.CustomStartCap = cap
       redPen.CustomEndCap = cap
       redPen.DashStyle = DashStyle.DashDotDot
       g.DrawLine(redPen, New Point(100, 100), New Point(400, 100))
       g.Dispose()
 End Sub
 Public Sub New()
  
   MyBase.New()
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(292, 273)
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
 End Sub

End Class</source>

Set Pen DashCap and DashStyle

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class PenDashStyle

  public Shared Sub Main
       Application.Run(New FrmDrawShapes)
  End Sub

End class Public Class FrmDrawShapes

  Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
  Public Sub New()
     MyBase.New()
     "This call is required by the Windows Form Designer.
     InitializeComponent()
     "Add any initialization after the InitializeComponent() call
  End Sub
  "Form overrides dispose to clean up the component list.
  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
  "Required by the Windows Form Designer
  Private components As System.ruponentModel.Container
  "NOTE: The following procedure is required by the Windows Form Designer
  "It can be modified using the Windows Form Designer.  
  "Do not modify it using the code editor.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
     "
     "frmDrawShapes
     "
     Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
     Me.ClientSize = New System.Drawing.Size(520, 197)
     Me.Name = "frmDrawShapes"
     Me.Text = "Drawing Shapes"
  End Sub
  1. End Region
  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
     Dim graphicsObject As Graphics = e.Graphics
     Dim solidColorBrush As SolidBrush = New SolidBrush(Color.Red)
     Dim coloredPen As Pen = New Pen(solidColorBrush)
     " draw a rounded, dashed yellow line
     coloredPen.Color = Color.Yellow
     coloredPen.Width = 6
     coloredPen.DashCap = LineCap.Round
     coloredPen.DashStyle = DashStyle.Dash
     graphicsObject.DrawLine(coloredPen, 320, 30, 395, 150)
  End Sub 

End Class</source>

Set Pen DashStyle, StartCap, EndCap, DashOffset

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class PenStyleStartEndCap

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

End class public class Form1

 Inherits System.Windows.Forms.Form
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       Dim g As Graphics = Me.CreateGraphics()
       g.Clear(Me.BackColor)
       Dim redPen As New Pen(New SolidBrush(Color.Red), 4)
       Dim bluePen As New Pen(New SolidBrush(Color.Blue), 5)
       Dim blackPen As New Pen(New SolidBrush(Color.Black), 3)
       redPen.DashStyle = DashStyle.Dash
       redPen.SetLineCap(LineCap.DiamondAnchor, LineCap.ArrowAnchor, DashCap.Flat)
       bluePen.DashStyle = DashStyle.DashDotDot
       bluePen.StartCap = LineCap.Triangle
       bluePen.EndCap = LineCap.Triangle
       bluePen.DashCap = DashCap.Triangle
       blackPen.DashStyle = DashStyle.Dot
       blackPen.DashOffset = 3.4F
       blackPen.SetLineCap(LineCap.RoundAnchor, LineCap.Square, DashCap.Round)
       " Drawing objects      
       g.DrawArc(redPen, 10.0F, 10.0F, 50, 100, 45.0F, 90.0F)
       g.DrawRectangle(bluePen, 60, 80, 140, 50)
       g.DrawBezier(blackPen, 20.0F, 30.0F, 100.0F, 200.0F, 40.0F, 400.0F, 100.0F, 200.0F)
       g.DrawEllipse(redPen, 50, 50, 200, 100)
       " Dispose
       redPen.Dispose()
       bluePen.Dispose()
       blackPen.Dispose()
       g.Dispose()
 End Sub
 Public Sub New()
  
   MyBase.New()
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(292, 273)
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
 End Sub

End Class</source>

Set Pen width and color

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class PenWidthColor

  public Shared Sub Main
       Application.Run(New FrmDrawShapes)
  End Sub

End class Public Class FrmDrawShapes

  Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
  Public Sub New()
     MyBase.New()
     "This call is required by the Windows Form Designer.
     InitializeComponent()
     "Add any initialization after the InitializeComponent() call
  End Sub
  "Form overrides dispose to clean up the component list.
  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
  "Required by the Windows Form Designer
  Private components As System.ruponentModel.Container
  "NOTE: The following procedure is required by the Windows Form Designer
  "It can be modified using the Windows Form Designer.  
  "Do not modify it using the code editor.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
     "
     "frmDrawShapes
     "
     Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
     Me.ClientSize = New System.Drawing.Size(520, 197)
     Me.Name = "frmDrawShapes"
     Me.Text = "Drawing Shapes"
  End Sub
  1. End Region
  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
     Dim graphicsObject As Graphics = e.Graphics
     Dim solidColorBrush As SolidBrush = New SolidBrush(Color.Red)
     Dim coloredPen As Pen = New Pen(solidColorBrush)
     coloredPen.Color = Color.White
     coloredPen.Width = 6
     graphicsObject.DrawPie(coloredPen, 240, 30, 75, 100, 0, 270)
  End Sub 

End Class</source>