Rectangle Structure

Represents a graphics location and size, expressed as a set of four integers. A Rectangle structure can be used to store the upper-left corner coordinates, the width, and the height of a rectangle.

Example:
Create a rectangle with its upper-left corner at (10, 20) and a width and height of 100 and 50 respectively.

using System.Drawing;

// ...

Rectangle rect = new Rectangle(10, 20, 100, 50);
// Or
Rectangle rect2 = new Rectangle(new Point(10, 20), new Size(100, 50));
                

Fields

Name Type Description
Empty static readonly Rectangle Represents a rectangle with no location or size. This is a read-only field.
X int Gets or sets the x-coordinate of the upper-left corner of this Rectangle structure.
Y int Gets or sets the y-coordinate of the upper-left corner of this Rectangle structure.
Width int Gets or sets the width of this Rectangle structure. Width must be non-negative.
Height int Gets or sets the height of this Rectangle structure. Height must be non-negative.

Constructors

Name Description
Rectangle(int x, int y, int width, int height) Initializes a new instance of the Rectangle structure with the specified location and size.
Rectangle(Point location, Size size) Initializes a new instance of the Rectangle structure with the specified location and size.

Properties

Name Type Description
Bottom int Gets the y-coordinate of the lower edge of this Rectangle structure.
Left int Gets the x-coordinate of the left edge of this Rectangle structure.
Right int Gets the x-coordinate of the right edge of this Rectangle structure.
Top int Gets the y-coordinate of the upper edge of this Rectangle structure.
Location Point Gets or sets the coordinates of the upper-left corner of this Rectangle structure.
Size Size Gets or sets the width and height of this Rectangle structure.
IsEmpty bool Gets a value indicating whether this Rectangle structure has a width of 0 and a height of 0 and a location at (0,0).

Methods

Name Description
Contains(int x, int y) Determines whether the specified point is contained within this Rectangle structure.
Contains(Point pt) Determines whether the specified point is contained within this Rectangle structure.
Inflate(int width, int height) Inflates this Rectangle by the specified amount.
Intersect(Rectangle rect1, Rectangle rect2) Returns a Rectangle structure that represents the intersection of two rectangles. If there is no intersection, an empty Rectangle is returned.
Offset(Point pos) Adjusts the position of this Rectangle by the specified offset.
Offset(int x, int y) Adjusts the position of this Rectangle by the specified offset.
Union(Rectangle rect1, Rectangle rect2) Returns a Rectangle structure that represents the union of two rectangles.
Note: Ensure that width and height values are non-negative.