Graphics.DrawRectangle Method

Definition

Draws a rectangle specified by a coordinate pair, a width, and a height.

Overloads

DrawRectangle(Pen, Int32, Int32, Int32, Int32)

Draws a rectangle specified by a coordinate pair, a width, and a height.

DrawRectangle(Pen, Single, Single, Single, Single)

Draws a rectangle specified by a coordinate pair, a width, and a height.

DrawRectangle(Pen, RectangleF)

Draws the outline of the specified rectangle.

DrawRectangle(Pen, Rectangle)

Draws a rectangle specified by a Rectangle structure.

DrawRectangle(Pen, Int32, Int32, Int32, Int32)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Draws a rectangle specified by a coordinate pair, a width, and a height.

C#
public void DrawRectangle(System.Drawing.Pen pen, int x, int y, int width, int height);

Parameters

pen
Pen

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

x
Int32

The x-coordinate of the upper-left corner of the rectangle to draw.

y
Int32

The y-coordinate of the upper-left corner of the rectangle to draw.

width
Int32

Width of the rectangle to draw.

height
Int32

Height of the rectangle to draw.

Exceptions

pen is null.

Examples

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

  • Creates a black pen.

  • Creates the position and size of a rectangle.

  • Draws the rectangle to the screen.

C#
public void DrawRectangleInt(PaintEventArgs e)
{
             
    // Create pen.
    Pen blackPen = new Pen(Color.Black, 3);
             
    // Create location and size of rectangle.
    int x = 0;
    int y = 0;
    int width = 200;
    int height = 200;
             
    // Draw rectangle to screen.
    e.Graphics.DrawRectangle(blackPen, x, y, width, height);
}

Remarks

For information about how to draw a RectangleF, see DrawRectangles(Pen, RectangleF[]).

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

DrawRectangle(Pen, Single, Single, Single, Single)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Draws a rectangle specified by a coordinate pair, a width, and a height.

C#
public void DrawRectangle(System.Drawing.Pen pen, float x, float y, float width, float height);

Parameters

pen
Pen

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

x
Single

The x-coordinate of the upper-left corner of the rectangle to draw.

y
Single

The y-coordinate of the upper-left corner of the rectangle to draw.

width
Single

The width of the rectangle to draw.

height
Single

The height of the rectangle to draw.

Exceptions

pen is null.

Examples

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

  • Creates a black pen.

  • Creates the position and size of a rectangle.

  • Draws the rectangle to the screen.

C#
public void DrawRectangleFloat(PaintEventArgs e)
{
             
    // Create pen.
    Pen blackPen = new Pen(Color.Black, 3);
             
    // Create location and size of rectangle.
    float x = 0.0F;
    float y = 0.0F;
    float width = 200.0F;
    float height = 200.0F;
             
    // Draw rectangle to screen.
    e.Graphics.DrawRectangle(blackPen, x, y, width, height);
}

Remarks

For information about how to draw a RectangleF, see DrawRectangles(Pen, RectangleF[]).

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

DrawRectangle(Pen, RectangleF)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Draws the outline of the specified rectangle.

C#
public void DrawRectangle(System.Drawing.Pen pen, System.Drawing.RectangleF rect);

Parameters

pen
Pen

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

rect
RectangleF

The rectangle to draw.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
Windows Desktop 7, 8, 9, 10

DrawRectangle(Pen, Rectangle)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Draws a rectangle specified by a Rectangle structure.

C#
public void DrawRectangle(System.Drawing.Pen pen, System.Drawing.Rectangle rect);

Parameters

pen
Pen

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

rect
Rectangle

A Rectangle structure that represents the rectangle to draw.

Exceptions

pen is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, 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.

C#
public void DrawRectangleRectangle(PaintEventArgs e)
{
             
    // Create pen.
    Pen blackPen = new Pen(Color.Black, 3);
             
    // Create rectangle.
    Rectangle rect = new Rectangle(0, 0, 200, 200);
             
    // Draw rectangle to screen.
    e.Graphics.DrawRectangle(blackPen, rect);
}

Remarks

For information about how to draw a RectangleF, see DrawRectangles(Pen, RectangleF[]).

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10