Export (0) Print
Expand All

RectangleF.Intersect Method (RectangleF, RectangleF)

Returns a RectangleF structure that represents the intersection of two rectangles. If there is no intersection, and empty RectangleF is returned.

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

public static RectangleF Intersect(
	RectangleF a,
	RectangleF b
)

Parameters

a
Type: System.Drawing.RectangleF

A rectangle to intersect.

b
Type: System.Drawing.RectangleF

A rectangle to intersect.

Return Value

Type: System.Drawing.RectangleF
A third RectangleF structure the size of which represents the overlapped area of the two specified rectangles.

This example is designed for use with Windows Forms, and it requires PaintEventArgs e, an OnPaint event object. The code creates two RectangleF objects and draws them to the screen in black and red. Notice that they have to be converted to Rectangle objects for drawing purposes. Then the code creates a third RectangleF using the Intersect method, converts it to a Rectangle, and draws it to the screen in blue. Notice the third (blue) rectangle is the area of overlap of the other two rectangles:

public void RectangleFIntersectExample(PaintEventArgs e)
{

    // Create two rectangles.
    RectangleF firstRectangleF = new RectangleF(0, 0, 75, 50);
    RectangleF secondRectangleF = new RectangleF(50, 20, 50, 50);

    // Convert the RectangleF structures to Rectangle structures and draw them to the 

    // screen.
    Rectangle firstRect = Rectangle.Truncate(firstRectangleF);
    Rectangle secondRect = Rectangle.Truncate(secondRectangleF);
    e.Graphics.DrawRectangle(Pens.Black, firstRect);
    e.Graphics.DrawRectangle(Pens.Red, secondRect);

    // Get the intersection.
    RectangleF intersectRectangleF =
        RectangleF.Intersect(firstRectangleF,
        secondRectangleF);

    // Draw the intersectRectangleF to the screen.
    Rectangle intersectRect =
        Rectangle.Truncate(intersectRectangleF);
    e.Graphics.DrawRectangle(Pens.Blue, intersectRect);
}

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft