Rectangle Constructor (Int32, Int32, Int32, Int32) (System.Drawing)

Switch View :
ScriptFree
.NET Framework Class Library
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)
Syntax

Visual Basic
Public Sub New ( _
	x As Integer, _
	y As Integer, _
	width As Integer, _
	height As Integer _
)
C#
public Rectangle(
	int x,
	int y,
	int width,
	int height
)
Visual C++
public:
Rectangle(
	int x, 
	int y, 
	int width, 
	int height
)
F#
new : 
        x:int * 
        y:int * 
        width:int * 
        height:int -> Rectangle

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.
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.

Visual Basic

Private Sub InstanceRectangleIntersection( _
    ByVal e As PaintEventArgs)

    Dim rectangle1 As New Rectangle(50, 50, 200, 100)
    Dim rectangle2 As New Rectangle(70, 20, 100, 200)

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

    If (rectangle1.IntersectsWith(rectangle2)) Then
        rectangle1.Intersect(rectangle2)
        If Not (rectangle1.IsEmpty) Then
            e.Graphics.FillRectangle(Brushes.Green, rectangle1)
        End If
    End If
End Sub


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


Visual C++

private:
   void InstanceRectangleIntersection( 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 ) )
      {
         rectangle1.Intersect( rectangle2 );
         if (  !rectangle1.IsEmpty )
         {
            e->Graphics->FillRectangle( Brushes::Green, rectangle1 );
         }
      }
   }


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 SP1
Platforms

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