How to: Draw an Outlined Shape

This example draws outlined ellipses and rectangles on a form.

    Private Sub DrawEllipse()
        Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
        Dim formGraphics As System.Drawing.Graphics
        formGraphics = Me.CreateGraphics()
        formGraphics.DrawEllipse(myPen, New Rectangle(0, 0, 200, 300))
        myPen.Dispose()
        formGraphics.Dispose()
    End Sub 

    Private Sub DrawRectangle()
        Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
        Dim formGraphics As System.Drawing.Graphics
        formGraphics = Me.CreateGraphics()
        formGraphics.DrawRectangle(myPen, New Rectangle(0, 0, 200, 300))
        myPen.Dispose()
        formGraphics.Dispose()
    End Sub

You cannot call this method in the Load event handler. The drawn content will not be redrawn if the form is resized or obscured by another form. To make your content automatically repaint, you should override the OnPaint method.

You should always call Dispose on any objects that consume system resources, such as Pen and Graphics objects.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft