Graphics.DrawRectangles Method

Definition

Draws a series of rectangles specified by Rectangle structures.

Overloads

DrawRectangles(Pen, Rectangle[])

Draws a series of rectangles specified by Rectangle structures.

DrawRectangles(Pen, RectangleF[])

Draws a series of rectangles specified by RectangleF structures.

DrawRectangles(Pen, ReadOnlySpan<Rectangle>)

Draws a series of rectangles specified by Rectangle structures.

DrawRectangles(Pen, ReadOnlySpan<RectangleF>)

Draws a series of rectangles specified by Rectangle structures.

DrawRectangles(Pen, Rectangle[])

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

Draws a series of rectangles specified by Rectangle structures.

C#
public void DrawRectangles(System.Drawing.Pen pen, params System.Drawing.Rectangle[] rects);
C#
public void DrawRectangles(System.Drawing.Pen pen, System.Drawing.Rectangle[] rects);

Parameters

pen
Pen

Pen that determines the color, width, and style of the outlines of the rectangles.

rects
Rectangle[]

Array of Rectangle structures that represent the rectangles to draw.

Exceptions

pen is null.

-or-

rects is null.

rects is a zero-length array.

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 an array of three rectangles.

  • Draws the rectangles to the screen.

C#
public void DrawRectanglesRectangle(PaintEventArgs e)
{
             
    // Create pen.
    Pen blackPen = new Pen(Color.Black, 3);
             
    // Create array of rectangles.
    Rectangle[] rects =
             {
                 new Rectangle( 0,   0, 100, 200),
                 new Rectangle(100, 200, 250,  50),
                 new Rectangle(300,   0,  50, 100)
             };
             
    // Draw rectangles to screen.
    e.Graphics.DrawRectangles(blackPen, rects);
}

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawRectangles(Pen, RectangleF[])

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

Draws a series of rectangles specified by RectangleF structures.

C#
public void DrawRectangles(System.Drawing.Pen pen, params System.Drawing.RectangleF[] rects);
C#
public void DrawRectangles(System.Drawing.Pen pen, System.Drawing.RectangleF[] rects);

Parameters

pen
Pen

Pen that determines the color, width, and style of the outlines of the rectangles.

rects
RectangleF[]

Array of RectangleF structures that represent the rectangles to draw.

Exceptions

pen is null.

-or-

rects is null.

rects is a zero-length array.

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 an array of three rectangles.

  • Draws the rectangles to the screen.

C#
public void DrawRectanglesRectangleF(PaintEventArgs e)
{
             
    // Create pen.
    Pen blackPen = new Pen(Color.Black, 3);
             
    // Create array of rectangles.
    RectangleF[] rects =
             {
                 new RectangleF( 0.0F,   0.0F, 100.0F, 200.0F),
                 new RectangleF(100.0F, 200.0F, 250.0F,  50.0F),
                 new RectangleF(300.0F,   0.0F,  50.0F, 100.0F)
             };
             
    // Draw rectangles to screen.
    e.Graphics.DrawRectangles(blackPen, rects);
}

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawRectangles(Pen, ReadOnlySpan<Rectangle>)

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

Draws a series of rectangles specified by Rectangle structures.

C#
public void DrawRectangles(System.Drawing.Pen pen, scoped ReadOnlySpan<System.Drawing.Rectangle> rects);

Parameters

pen
Pen

Pen that determines the color, width, and style of the outlines of the rectangles.

rects
ReadOnlySpan<Rectangle>

Array of Rectangle structures that represent the rectangles to draw.

Applies to

.NET 9 (package-provided) and Windows Desktop 9
Product Versions
.NET 9 (package-provided)
Windows Desktop 9

DrawRectangles(Pen, ReadOnlySpan<RectangleF>)

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

Draws a series of rectangles specified by Rectangle structures.

C#
public void DrawRectangles(System.Drawing.Pen pen, scoped ReadOnlySpan<System.Drawing.RectangleF> rects);

Parameters

pen
Pen

Pen that determines the color, width, and style of the outlines of the rectangles.

rects
ReadOnlySpan<RectangleF>

Array of Rectangle structures that represent the rectangles to draw.

Applies to

.NET 9 (package-provided) and Windows Desktop 9
Product Versions
.NET 9 (package-provided)
Windows Desktop 9