Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Rectangle Constructor (Int32, Int32, Int32, Int32)

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

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

public Rectangle(
	int x,
	int y,
	int width,
	int height
)

Parameters

x
Type: System.Int32

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

y
Type: System.Int32

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

width
Type: System.Int32

The width of the rectangle.

height
Type: System.Int32

The height of the rectangle.

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.

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);
        }
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show:
© 2015 Microsoft