DrawRectangle Method (Pen, Rectangle)

Graphics.DrawRectangle Method (Pen, Rectangle)


Draws a rectangle specified by a Rectangle structure.

Namespace:   System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)

Public Sub DrawRectangle (
	pen As Pen,
	rect As Rectangle


Type: System.Drawing.Pen

A Pen that determines the color, width, and style of the rectangle.

Type: System.Drawing.Rectangle

A Rectangle structure that represents the rectangle to draw.

Exception Condition

pen is null.

For information about how to draw a RectangleF, seeDrawRectangles(Pen, RectangleF()).

The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a black pen.

  • Creates a rectangle.

  • Draws the rectangle to the screen.

Public Sub DrawRectangleRectangle(ByVal e As PaintEventArgs)

    ' Create pen.
    Dim blackPen As New Pen(Color.Black, 3)

    ' Create rectangle.
    Dim rect As New Rectangle(0, 0, 200, 200)

    ' Draw rectangle to screen.
    e.Graphics.DrawRectangle(blackPen, rect)
End Sub

.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft