This value type defines a rectangular region on a 2D surface using
integers. Unlike the Win32 RECT structure, the
Rectangle is defined in terms of the
X and Y coordinate of its
top-left point (also referred to as its Location), and its
Width and Height (also referred
to as its Size). These properties can be both read and modified. In
addition, you can read values for the Left,
Top, Right, and
Bottom independently, and there is a static
method, FromLTRB(), to allow you to construct a
Rectangle from these values in interop/legacy
situations.
As with Point, there is an equivalent
RectangleF structure for real coordinates, and a
set of members—Round(), Truncate(), and Ceiling()—to convert from
the real to the integer representation. Comparison operators are also
provided, along with an Empty field to allow you
to represent a null rectangle. There is no intrinsic performance
advantage in using the integer version, as the drawing methods are
mostly implemented in terms of the floating point structure anyway
(c.f. Point/PointF)
Methods are provided to test whether a rectangle Contains() a Point, and whether one rectangle
IntersectsWith() another for hit testing.
You can also manipulate the rectangle itself in various ways. There
is a static member, Union(), which returns the
minimum containing rectangle of two rectangles. Then there is the
member function, Intersect(), which modifies a
Rectangle such that it represents the area of
intersection between itself and the rectangle supplied as its
parameter.
Finally, there are methods to Offset() the
rectangle by a fixed amount, or Inflate() the
rectangle. There are both static and non-static versions of
Inflate() to return a new rectangle or modify the
original, respectively. You can independently control the inflation
in each dimension, and the value in question is subtracted from the
Location and added to the Size,
leading to an overall increase of twice the supplied value in each
dimension.
public struct Rectangle {
// Public Constructors
public Rectangle(int x, int y, int width, int height);
public Rectangle(Point location, Size size);
// Public Static Fields
public static readonly Rectangle Empty; // = {X=0,Y=0,Width=0,Height=0}
// Public Instance Properties
public int Bottom{get; }
public int Height{set; get; }
public bool IsEmpty{get; }
public int Left{get; }
public Point Location{set; get; }
public int Right{get; }
public Size Size{set; get; }
public int Top{get; }
public int Width{set; get; }
public int X{set; get; }
public int Y{set; get; }
// Public Static Methods
public static Rectangle Ceiling(RectangleF value);
public static Rectangle FromLTRB(int left, int top, int right, int bottom);
public static Rectangle Inflate(Rectangle rect, int x, int y);
public static Rectangle Intersect(Rectangle a, Rectangle b);
public static Rectangle Round(RectangleF value);
public static Rectangle Truncate(RectangleF value);
public static Rectangle Union(Rectangle a, Rectangle b);
public static bool operator !=(Rectangle left, Rectangle right);
public static bool operator ==(Rectangle left, Rectangle right);
// Public Instance Methods
public bool Contains(int x, int y);
public bool Contains(Point pt);
public bool Contains(Rectangle rect);
public override bool Equals(object obj); // overrides ValueType
public override int GetHashCode(); // overrides ValueType
public void Inflate(int width, int height);
public void Inflate(Size size);
public void Intersect(Rectangle rect);
public bool IntersectsWith(Rectangle rect);
public void Offset(int x, int y);
public void Offset(Point pos);
public override string ToString(); // overrides ValueType
}