Rectangle.Intersect Method

Definition

Determines the Rectangle structure that represents the intersection of two rectangles.

Overloads

Intersect(Rectangle, Rectangle)

Returns a third Rectangle structure that represents the intersection of two other Rectangle structures. If there is no intersection, an empty Rectangle is returned.

Intersect(Rectangle)

Replaces this Rectangle with the intersection of itself and the specified Rectangle.

Intersect(Rectangle, Rectangle)

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

Returns a third Rectangle structure that represents the intersection of two other Rectangle structures. If there is no intersection, an empty Rectangle is returned.

C#
public static System.Drawing.Rectangle Intersect(System.Drawing.Rectangle a, System.Drawing.Rectangle b);

Parameters

a
Rectangle

A rectangle to intersect.

b
Rectangle

A rectangle to intersect.

Returns

A Rectangle that represents the intersection of a and b.

Examples

The following code example demonstrates the Intersect, IsEmpty and the 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 StaticRectangleIntersection(PaintEventArgs e)
{
    Rectangle rectangle1 = new Rectangle(50, 50, 200, 100);
    Rectangle rectangle2 = new Rectangle(70, 20, 100, 200);
    Rectangle rectangle3 = new Rectangle();

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

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

Applies to

.NET 10 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, 10
.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

Intersect(Rectangle)

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

Replaces this Rectangle with the intersection of itself and the specified Rectangle.

C#
public void Intersect(System.Drawing.Rectangle rect);

Parameters

rect
Rectangle

The Rectangle with which to intersect.

Examples

The following code example demonstrates the Intersect, IsEmpty and the 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 10 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, 10
.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