Represents a rectangle defined by a location and size.
System.Drawing
System.Drawing.dll
public struct Rectangle : System.ICloneable, System.IEquatable<Rectangle>
The Rectangle
structure represents a rectangle in graphics and user interface operations. A rectangle is defined by its location (top-left corner) and its size (width and height).
Instances of the Rectangle
structure are used to define clipping regions, control the positioning of controls, and perform various geometric operations.
Name | Description |
---|---|
Rectangle(Point location, Size size)
|
Initializes a new instance of the Rectangle structure with the specified location and size. |
Rectangle(int x, int y, int width, int height)
|
Initializes a new instance of the Rectangle structure with the specified location and size. |
Name | Type | Description |
---|---|---|
Location |
Point |
Gets or sets the coordinates of the upper-left corner of the rectangle structure. |
Size |
Size |
Gets or sets the width and height of the rectangle structure. |
X |
int |
Gets or sets the x-coordinate of the upper-left corner of the rectangle structure. |
Y |
int |
Gets or sets the y-coordinate of the upper-left corner of the rectangle structure. |
Width |
int |
Gets or sets the width of the rectangle structure. |
Height |
int |
Gets or sets the height of the rectangle structure. |
Left |
int |
Gets the x-coordinate of the left edge of the rectangle structure. |
Top |
int |
Gets the y-coordinate of the top edge of the rectangle structure. |
Right |
int |
Gets the x-coordinate of the right edge of the rectangle structure. |
Bottom |
int |
Gets the y-coordinate of the bottom edge of the rectangle structure. |
IsEmpty |
bool |
Gets a value indicating whether the rectangle structure has a width of 0 or height of 0. |
Name | Description |
---|---|
Clone()
|
Creates an object that is a copy of the current instance. |
Contains(Point pt)
|
Checks whether the specified point is within the rectangular region of this rectangle. |
Contains(int x, int y)
|
Checks whether the specified point is within the rectangular region of this rectangle. |
Contains(Rectangle rect)
|
Checks whether the specified rectangle is within the rectangular region of this rectangle. |
Equals(object obj)
|
Determines whether the specified object is a Rectangle structure with the same location and size as this Rectangle structure. |
Equals(Rectangle other)
|
Determines whether the specified rectangle is a Rectangle structure with the same location and size as this Rectangle structure. |
Inflate(int inflatment)
|
Inflates this rectangle by the specified amount on all sides. |
Inflate(int width, int height)
|
Inflates this rectangle by the specified amount on all sides. |
Intersect(Rectangle rect1, Rectangle rect2)
|
Returns a Rectangle structure that represents the intersection of two rectangles. |
IntersectsWith(Rectangle r)
|
Checks whether the specified rectangle intersects with this rectangle. |
Offset(Point pos)
|
Re positions the Rectangle by the specified offset. |
Offset(int dx, int dy)
|
Re positions the Rectangle by the specified offset. |
ToString()
|
Returns a string that represents the current object. |
Operator | Description |
---|---|
== (Rectangle r1, Rectangle r2)
|
Compares two Rectangle structures for equality. |
!= (Rectangle r1, Rectangle r2)
|
Compares two Rectangle structures for inequality. |
The following code example demonstrates how to create and manipulate Rectangle
objects.
// Using directives using System.Drawing; public class Example { public static void Main() { // Create a rectangle Rectangle rect1 = new Rectangle(10, 20, 100, 50); // Access properties Point location = rect1.Location; int width = rect1.Width; int height = rect1.Height; // Move the rectangle rect1.Offset(30, 40); // Check if a point is inside Point testPoint = new Point(25, 35); if (rect1.Contains(testPoint)) { Console.WriteLine("The point is inside the rectangle."); } // Create another rectangle Rectangle rect2 = new Rectangle(50, 60, 80, 70); // Find the intersection Rectangle intersection = Rectangle.Intersect(rect1, rect2); Console.WriteLine($"Intersection: {intersection}"); } }