Rectangle Constructors

Definition

Initializes a new instance of the Rectangle class with the specified location and size.

Overloads

Rectangle(Point, Size)

Initializes a new instance of the Rectangle class with the specified location and size.

Rectangle(Int32, Int32, Int32, Int32)

Initializes a new instance of the Rectangle class with the specified location and size.

Rectangle(Point, Size)

Source:
Rectangle.cs
Source:
Rectangle.cs
Source:
Rectangle.cs

Initializes a new instance of the Rectangle class with the specified location and size.

C#
public Rectangle(System.Drawing.Point location, System.Drawing.Size size);

Parameters

location
Point

A Point that represents the upper-left corner of the rectangular region.

size
Size

A Size that represents the width and height of the rectangular region.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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, 2.1

Rectangle(Int32, Int32, Int32, Int32)

Source:
Rectangle.cs
Source:
Rectangle.cs
Source:
Rectangle.cs

Initializes a new instance of the Rectangle class with the specified location and size.

C#
public Rectangle(int x, int y, int width, int height);

Parameters

x
Int32

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

y
Int32

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

width
Int32

The width of the rectangle.

height
Int32

The height of the rectangle.

Examples

The following code example demonstrates the Rectangle, Intersect, IsEmpty, and IntersectsWith members. This example should be used with a Windows Form. Paste this code into a form and call this method when handling the form's Paint event, passing e as PaintEventArgs.

C#
private void InstanceRectangleIntersection(PaintEventArgs e)
{

    Rectangle rectangle1 = new Rectangle(50, 50, 200, 100);
    Rectangle rectangle2 = new Rectangle(70, 20, 100, 200);

    e.Graphics.DrawRectangle(Pens.Black, rectangle1);
    e.Graphics.DrawRectangle(Pens.Red, rectangle2);

    if (rectangle1.IntersectsWith(rectangle2))
    {
        rectangle1.Intersect(rectangle2);
        if (!rectangle1.IsEmpty)
        {
            e.Graphics.FillRectangle(Brushes.Green, rectangle1);
        }
    }
}

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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, 2.1