Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleDemonstrate the Graphics object's drawing methods in VB .NET
DescriptionThis example shows how to demonstrate the Graphics object's drawing methods in VB .NET.
Keywordsdraw, drawing, DrawLine, DrawLines, DrawPath, DrawEllipse, DrawPie, DrawRectangle, DrawRectangles, DrawArc, DrawPolygon, DrawString, DrawBezier, DrawBeziers, DrawClosedCurve, DrawCurve, DrawImage
CategoriesGraphics, VB.NET
 
This program demonstrates the Graphics object's drawing methods: DrawLine, DrawLines, DrawPath, DrawEllipse, DrawPie, DrawRectangle, DrawRectangles, DrawArc, DrawPolygon, DrawString, DrawBezier, DrawBeziers, DrawClosedCurve, DrawCurve, and DrawImage.
 
Imports System.Drawing.Drawing2D
...
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    ' Draw a line.
    e.Graphics.DrawLine(Pens.Black, 10, 10, 30, 20)

    ' Draw a series of connected lines.
    Dim pts() As Point = { _
        New Point(10, 20), _
        New Point(30, 30), _
        New Point(50, 10), _
        New Point(30, 10), _
        New Point(50, 30) _
    }
    e.Graphics.DrawLines(Pens.Blue, pts)

    ' Draw a path.
    ' Make the path.
    Dim graphics_path As New GraphicsPath
    graphics_path.AddLine(10, 40, 60, 40)
    graphics_path.AddEllipse(60, 30, 30, 20)
    e.Graphics.DrawPath(Pens.Red, graphics_path)

    ' Draw an ellipse and a pie slice on it.
    Dim rect As New Rectangle(10, 70, 40, 30)
    e.Graphics.DrawEllipse(Pens.White, rect)
    e.Graphics.DrawPie(Pens.Green, rect, 10, 45)

    ' Draw an ellipse and an arc on it.
    rect = New Rectangle(60, 70, 40, 30)
    e.Graphics.DrawEllipse(Pens.White, rect)
    e.Graphics.DrawArc(Pens.Green, rect, 10, 45)

    ' Draw a polygon.
    pts = New Point() { _
        New Point(10, 100), _
        New Point(80, 140), _
        New Point(80, 90), _
        New Point(10, 130) _
    }
    e.Graphics.DrawPolygon(Pens.Yellow, pts)

    ' Draw a rectangle.
    rect = New Rectangle(10, 150, 50, 20)
    e.Graphics.DrawRectangle(Pens.Brown, rect)

    ' Draw some rectangles.
    Dim rects() As Rectangle = { _
        New Rectangle(10, 190, 20, 30), _
        New Rectangle(20, 200, 40, 10), _
        New Rectangle(40, 180, 10, 50) _
    }
    e.Graphics.DrawRectangles(Pens.Aquamarine, rects)

    ' Draw some text.
    e.Graphics.DrawString("Hello!", _
        Me.Font, Brushes.Chocolate, 10, 250)

    ' Draw a Bezier curve.
    Dim ptfs() As PointF = { _
        New PointF(150, 25), _
        New PointF(185, 10), _
        New PointF(200, 50), _
        New PointF(160, 30) _
    }
    e.Graphics.DrawLines(Pens.LightGray, ptfs)
    For Each ptf As PointF In ptfs
        rect = New Rectangle(CInt(ptf.X - 2), CInt(ptf.Y - _
            2), 4, 4)
        e.Graphics.DrawRectangle(Pens.LightGray, rect)
    Next ptf
    e.Graphics.DrawBezier(Pens.Black, _
        ptfs(0), ptfs(1), ptfs(2), ptfs(3))

    ' Draw a series of connected Bezier curves.
    ptfs = New PointF() { _
        New PointF(150, 75), _
        New PointF(185, 60), _
        New PointF(200, 100), _
        New PointF(160, 80), _
        New PointF(160, 120), _
        New PointF(200, 140), _
        New PointF(220, 90) _
    }
    e.Graphics.DrawLines(Pens.LightGray, ptfs)
    For Each ptf As PointF In ptfs
        rect = New Rectangle(CInt(ptf.X - 2), CInt(ptf.Y - _
            2), 4, 4)
        e.Graphics.DrawRectangle(Pens.LightGray, rect)
    Next ptf
    e.Graphics.DrawBeziers(Pens.Orange, ptfs)

    ' Draw a closed curve.
    pts = New Point() { _
        New Point(150, 150), _
        New Point(200, 200), _
        New Point(180, 150), _
        New Point(130, 200) _
    }
    e.Graphics.DrawClosedCurve(Pens.BlueViolet, pts)

    ' Draw a curve (open).
    pts = New Point() { _
        New Point(150, 210), _
        New Point(200, 260), _
        New Point(180, 210), _
        New Point(130, 260) _
    }
    e.Graphics.DrawCurve(Pens.CornflowerBlue, pts)

    ' Draw an ellipse.
    rect = New Rectangle(230, 10, 50, 30)
    e.Graphics.DrawEllipse(Pens.DarkMagenta, rect)

    ' Draw an image.
    e.Graphics.DrawImage(picHidden.Image, 230, 50)
    e.Graphics.DrawImage(picHidden.Image, 250, 50, 32, 32)
    Dim dst_rect As New Rectangle(290, 50, 20, 20)
    Dim src_rect As New Rectangle(4, 4, 8, 8)
    e.Graphics.DrawImage(picHidden.Image, dst_rect, _
        src_rect, GraphicsUnit.Pixel)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated