.NET Framework Class Library
Rectangle.Intersect Method (Rectangle, Rectangle)
Namespace:
System.Drawing
Assembly: System.Drawing (in System.Drawing.dll)
Syntax
Visual Basic
Public Shared Function Intersect ( _ a As Rectangle, _ b As Rectangle _ ) As Rectangle
C#
public static Rectangle Intersect( Rectangle a, Rectangle b )
Visual C++
public: static Rectangle Intersect( Rectangle a, Rectangle b )
F#
static member Intersect : a:Rectangle * b:Rectangle -> Rectangle
Parameters
- a
- Type: System.Drawing.Rectangle
A rectangle to intersect.
- b
- Type: System.Drawing.Rectangle
A rectangle 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.
Visual Basic
Private Sub StaticRectangleIntersection(ByVal e As PaintEventArgs) Dim rectangle1 As New Rectangle(50, 50, 200, 100) Dim rectangle2 As New Rectangle(70, 20, 100, 200) Dim rectangle3 As New Rectangle e.Graphics.DrawRectangle(Pens.Black, rectangle1) e.Graphics.DrawRectangle(Pens.Red, rectangle2) If (rectangle1.IntersectsWith(rectangle2)) Then rectangle3 = Rectangle.Intersect(rectangle1, rectangle2) If Not rectangle3.IsEmpty Then e.Graphics.FillRectangle(Brushes.Green, rectangle3) End If End If End Sub
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); } } }
Visual C++
private: void StaticRectangleIntersection( PaintEventArgs^ e ) { Rectangle rectangle1 = Rectangle(50,50,200,100); Rectangle rectangle2 = Rectangle(70,20,100,200); e->Graphics->DrawRectangle( Pens::Black, rectangle1 ); e->Graphics->DrawRectangle( Pens::Red, rectangle2 ); if ( rectangle1.IntersectsWith( rectangle2 ) ) { Rectangle rectangle3 = Rectangle::Intersect( rectangle1, rectangle2 ); if ( !rectangle3.IsEmpty ) { e->Graphics->FillRectangle( Brushes::Green, rectangle3 ); } } }
Version Information
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0.NET Framework Client Profile
Supported in: 4, 3.5 SP1Platforms
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
See Also
Reference
Community Content
cyclotis04
Use caution with negatively-sized rectangles
Rectangles do not intersect as expected when given negative sizes. Consider redefining them manually to positive sizes before using this method.
var
r
=
new
Rectangle
(
0
,
0
,
3
,
3
);
var
r0
=
new
Rectangle
(
0
,
0
,
-
1
,
-
1
);
var
r1
=
new
Rectangle
(
1
,
1
,
-
1
,
-
1
);
var
r2
=
new
Rectangle
(
2
,
2
,
-
1
,
-
1
);
var
r3
=
new
Rectangle
(
3
,
3
,
-
1
,
-
1
);
System
.
Console
.
WriteLine
(
r
.
IntersectsWith
(
r0
));
// False
System
.
Console
.
WriteLine
(
r
.
IntersectsWith
(
r1
));
// False
System
.
Console
.
WriteLine
(
r
.
IntersectsWith
(
r2
));
// True
System
.
Console
.
WriteLine
(
r
.
IntersectsWith
(
r3
));
// False