Rectangle

References
[TypeConverter(RectangleConverterInternal)] public struct Rectangle : IEquatable<Rectangle>
Represents a rectangle with a location (X, Y) and size (Width, Height) components.
Remarks: A rectangle is defined by a location (X, Y) and a size (Width, Height). The width and/or height can be negative.

Constructors

Rectangle

#
public Rectangle(int x, int y, int width, int height)
Initializes a new instance of the Rectangle class with the specified x, y, width, and height
x (int)X co-ordinate for the location of the rectangle
y (int)Y co-ordinate for the location of the rectangle
width (int)Width of the rectangle
height (int)Height of the rectangle

Rectangle

#
public Rectangle(Point location, Size size)
Initializes a new instance of the Rectangle class with the specified location and size

Rectangle

#
public Rectangle(Point start, Point end)
Initializes a new instance of the Rectangle class with two points
start (Point)Starting point of the rectangle
end (Point)Ending point of the rectangle

Rectangle

#
public Rectangle(RectangleF rectangle)
Initializes a new instance of a Rectangle class with truncated values of the specified floating-point rectangle
rectangle (RectangleF)RectangleF to initialize the X and Y values of the new instance with

Rectangle

#
public Rectangle(Size size)
Initializes a new instance of the Rectangle class with X, Y co-ordinates at 0,0 and the specified size

Fields

Empty

#
public static readonly Rectangle Empty
Gets an empty rectangle with zero X, Y, Width, and Height components

Properties

Bottom

#
public int Bottom { get; set; }
Gets or sets the logical bottom of this rectangle (Y + Height if Height is positive, Y + 1 if negative)
Remarks: This differs from the InnerBottom in that this will return the co-ordinate adjacent to the bottom edge of the rectangle, whereas InnerBottom returns the co-ordinate that is inside the rectangle

BottomLeft

#
public Point BottomLeft { get; set; }
Gets or sets the point at the Bottom and Left of the rectangle

BottomRight

#
public Point BottomRight { get; set; }
Gets or sets the point at the Bottom and Right of the rectangle

Center

#
public Point Center { get; set; }
Gets or sets the rectangle's center position

EndLocation

#
public Point EndLocation { get; set; }
Gets the ending location of this rectangle
Remarks: This gets/sets the product of the Location + Size. If the Width or Height of this rectangle is positive, then the X/Y of the returned location will be minus 1 so as to be inside of the rectangle's bounds.

Height

#
public int Height { get; set; }
Gets or sets the Height of this rectangle

InnerBottom

#
public int InnerBottom { get; set; }
Gets or sets the bottom of the rectangle that is inside the bounds
Remarks: Similar to Bottom but inside the rectangle's bounds instead of just below the bottom

InnerBottomLeft

#
public Point InnerBottomLeft { get; set; }
Gets or sets the point at the InnerBottom and Left of the rectangle
Remarks: Similar to BottomLeft but inside the rectangle's bounds instead of just below the bottom

InnerBottomRight

#
public Point InnerBottomRight { get; set; }
Gets or sets the point at the InnerBottom and InnerRight of the rectangle
Remarks: Similar to BottomRight but inside the rectangle's bounds instead of just to the right and bottom

InnerRight

#
public int InnerRight { get; set; }
Gets or sets the right of the rectangle that is inside the bounds
Remarks: Similar to Right but inside the rectangle's bounds instead of just to the right

InnerTopRight

#
public Point InnerTopRight { get; set; }
Gets or sets the point at the Top and InnerRight of the rectangle
Remarks: Similar to TopRight but inside the rectangle's bounds instead of just to the right

IsEmpty

#
public bool IsEmpty { get; }
Gets a value indicating that the Size of this rectangle is empty (either the width or height are zero)

IsZero

#
public bool IsZero { get; }
Gets a value indicating that both the and Size of this rectangle are zero
Remarks: The X, Y, Width, and Height components of this rectangle must be zero for this to return true.

Left

#
public int Left { get; set; }
Gets or sets the logical left of this rectangle (X co-ordinate if Width is positive, X + Width if negative)

Location

#
public Point Location { get; set; }
Gets the location of this rectangle

MiddleBottom

#
public Point MiddleBottom { get; set; }
Gets or sets the point at the MiddleX and Bottom of the rectangle

MiddleLeft

#
public Point MiddleLeft { get; set; }
Gets or sets the point at the Left and MiddleY of the rectangle

MiddleRight

#
public Point MiddleRight { get; set; }
Gets or sets the point at the Right and MiddleY of the rectangle

MiddleTop

#
public Point MiddleTop { get; set; }
Gets or sets the point at the MiddleX and Top of the rectangle

MiddleX

#
public int MiddleX { get; set; }
Gets or sets the rectangle's middle horizontal position

MiddleY

#
public int MiddleY { get; set; }
Gets or sets the rectangle's middle vertical position

Top

#
public int Top { get; set; }
Gets or sets the logical top of this rectangle (Y co-ordinate if Height is positive, Y + Height if negative)
Remarks: This is always the logical top, where if the Height is positive it will adjust the Y co-ordinate. If the Height of the rectangle is negative, then this will adjust the Height when setting the value.

TopLeft

#
public Point TopLeft { get; set; }
Gets or sets the point at the Top and Left of the rectangle

TopRight

#
public Point TopRight { get; set; }
Gets or sets the point at the Top and Right of the rectangle

X

#
public int X { get; set; }
Gets or sets the X co-ordinate of the Location of this rectangle

Y

#
public int Y { get; set; }
Gets or sets the Y co-ordinate of the Location of this rectangle

Methods

Align

#
public static Rectangle Align(Rectangle rectangle, int gridWidth, int gridHeight)
Aligns the rectangle to a grid of the specified gridWidth and gridHeight
Remarks: This will align the top, left, right, and bottom to a grid by inflating each edge to the next grid line.
rectangle (Rectangle)Rectangle to align
gridWidth (int)Grid width
gridHeight (int)Grid height
A new Rectangle aligned to the grid

Align

#
public static Rectangle Align(Rectangle rectangle, Size gridSize)
Aligns the rectangle to a grid of the specified gridSize
Remarks: This will align the top, left, right, and bottom to a grid by inflating each edge to the next grid line.
rectangle (Rectangle)Rectangle to align
gridSize (Size)Size of the grid to align the rectangle to
A new Rectangle aligned to the grid

Align

#
public void Align(int gridWidth, int gridHeight)
Aligns the rectangle to a grid of the specified gridWidth and gridHeight
Remarks: This will align the top, left, right, and bottom to a grid by inflating each edge to the next grid line.
gridWidth (int)Grid width
gridHeight (int)Grid height

Align

#
public void Align(Size gridSize)
Aligns the rectangle to a grid of the specified gridSize
Remarks: This will align the top, left, right, and bottom to a grid by inflating each edge to the next grid line.
gridSize (Size)Size of the grid to align the rectangle to

Ceiling

#
public static Rectangle Ceiling(RectangleF rectangle)
Converts a floating point rectangle to an integral Rectangle by getting the smallest integral value of X, Y, Width, and Height
Remarks: This is used to get an integral rectangle that bounds the floating point rectangle completely. E.g. if a RectangleF has (X,Y,W,H) values of 0.2, 0.9, 1.1, 1.9, it would return a new integral rectangle with values 0, 0, 2, 2.
rectangle (RectangleF)Rectangle to get the ceiling
A new instance of a Rectangle with truncated X, Y and a ceiling Width and Height values

Contains

#
public bool Contains(int x, int y)
Gets a value indicating that the specified x and y co-ordinates are within the bounds of this rectangle
x (int)X co-ordinate to test
y (int)Y co-ordinate to test
True if the rectangle contains the x and y co-ordinates, false otherwise

Contains

#
public bool Contains(Point point)
Gets a value indicating that the specified point is within the bounds of this rectangle
point (Point)Point to test
True if the point is within the bounds of this rectangle, false if it is outside the bounds

Contains

#
public bool Contains(Rectangle rectangle)
Gets a value indicating that the specified rectangle is entirely contained within the bounds of this rectangle
rectangle (Rectangle)Rectangle to test if it is contained within this instance

Equals

#
public override bool Equals(object obj)
Compares this rectangle to an object for equality
obj (object)Object to compare with
True if the obj is a Rectangle and is equal to this instance, false otherwise
ValueType.Equals

Equals

#
public bool Equals(Rectangle other)
Compares this rectangle with the specified other rectangle
other (Rectangle)Other rectangle to compare with
True if the other rectangle is equal to this instance, false otherwise

FromCenter

#
public static Rectangle FromCenter(Point center, Size size)
Creates a new instance of a Rectangle with a specified center and size
center (Point)Center of the rectangle
size (Size)Size of the rectangle
A new instance of a Rectangle with the specified center and size

FromSides

#
public static Rectangle FromSides(int left, int top, int right, int bottom)
Creates a new instance of a RectangleF from the values of the left, top, right and bottom sides
left (int)Left side of the rectangle to create
top (int)Top of the rectangle to create
right (int)Right side of the rectangle to create
bottom (int)Bottom of the rectangle to create
A new instance of a RectangleF with values for the Left, Top, Right, and Bottom sides

GetHashCode

#
public override int GetHashCode()
Gets the hash code for this rectangle

Inflate

#
public static Rectangle Inflate(Rectangle rectangle, int width, int height)
Inflates all dimensions of this rectangle by the specified width and height
Remarks: This inflates the rectangle in all dimensions by the specified width and height. The resulting rectangle will be increased in width and height twice that of the specified size, and the center will be in the same location. A negative width and/or height can be passed in to deflate the rectangle.
rectangle (Rectangle)Rectangle to inflate
width (int)Width to inflate the left and right of the rectangle by
height (int)Height to inflate the top and bottom of the rectangle by
A new rectangle inflated by the specified width and height

Inflate

#
public static Rectangle Inflate(Rectangle rectangle, Size size)
Inflates all dimensions of the rectangle by the specified size
Remarks: This inflates the rectangle in all dimensions by the width and height specified by size. The resulting rectangle will be increased in width and height twice that of the specified size, and the center will be in the same location. A negative width and/or height can be passed in to deflate the rectangle.
rectangle (Rectangle)Rectangle to inflate
size (Size)Size to inflate the rectangle by
A new rectangle inflated by the specified width and height

Inflate

#
public void Inflate(int width, int height)
Inflates all dimensions of this rectangle by the specified width and height
Remarks: This inflates the rectangle in all dimensions by the specified width and height. The resulting rectangle will be increased in width and height twice that of the specified size, and the center will be in the same location. A negative width and/or height can be passed in to deflate the rectangle.
width (int)Width to inflate the left and right of the rectangle by
height (int)Height to inflate the top and bottom of the rectangle by

Inflate

#
public void Inflate(Size size)
Inflates all dimensions of this rectangle by the specified size
Remarks: This inflates the rectangle in all dimensions by the width and height specified by size. The resulting rectangle will be increased in width and height twice that of the specified size, and the center will be in the same location. A negative width and/or height can be passed in to deflate the rectangle.
size (Size)Size to inflate the rectangle by

Inset

#
public static Rectangle Inset(Rectangle rectangle, Padding padding)
Returns a new rectangle that is inset by the specified padding
rectangle (Rectangle)Rectangle to inset
padding (Padding)Padding to inset by
A new rectangle inset by the specified padding

Inset

#
public void Inset(Padding padding)
Insets the rectangle by the specified padding.

Intersect

#
public static Rectangle Intersect(Rectangle rect1, Rectangle rect2)
Intersect the two specified rectangles
rect1 (Rectangle)First rectangle to intersect
rect2 (Rectangle)Second rectangle to intersect
A new RectangleF with the intersection of the two rectangles

Intersect

#
public void Intersect(Rectangle rectangle)
Intersect the rectangle with the specified rectangle

Intersects

#
public bool Intersects(Rectangle rectangle)
Gets a value indicating that the specified rectangle overlaps this rectangle
rectangle (Rectangle)Rectangle to test for intersection/overlap
True if the rectangle overlaps this instance, false otherwise

Normalize

#
public void Normalize()
Normalizes the rectangle so both the Width and Height are positive, without changing the location of the rectangle
Remarks: Rectangles can have negative widths/heights, which means that the starting location will not always be at the top left corner. Normalizing the rectangle will ensure that the X and Y co-ordinates of the rectangle are at the top left.

Offset

#
public static Rectangle Offset(Rectangle rectangle, int x, int y)
Offsets the location of the rectangle by the specified x and y values
rectangle (Rectangle)Rectangle to offset
x (int)Horizontal offset to move the rectangle
y (int)Vertical offset to move the rectangle
A new Rectangle with the offset location

Offset

#
public static Rectangle Offset(Rectangle rectangle, Point point)
Offsets the location of the rectangle by the X and Y values of the specified point
rectangle (Rectangle)Rectangle to offset
point (Point)Point with values to offset the rectangle
A new Rectangle with the offset location

Offset

#
public static Rectangle Offset(Rectangle rectangle, Size size)
Offsets the location of the rectangle by the specified size
rectangle (Rectangle)Rectangle to inflate
size (Size)Width and Height to move the rectangle
A new Rectangle with the offset location

Offset

#
public void Offset(int x, int y)
Offsets the location of the rectangle by the specified x and y values
x (int)Horizontal offset to move the rectangle
y (int)Vertical offset to move the rectangle

Offset

#
public void Offset(Point point)
Offsets the location of the rectangle by the X and Y values of the specified point

Offset

#
public void Offset(Size size)
Offsets the location of the rectangle by the specified size

Restrict

#
public static Rectangle Restrict(Rectangle rectangle, Rectangle restrict)
Restricts the rectangle to be within the restrict rectangle
Remarks: This ensures that rectangle's bounds fall within the bounds of the specified restrict rectangle It is useful to ensure that the rectangle does not exceed certain limits (e.g. for drawing)
rectangle (Rectangle)Rectangle to restrict
restrict (Rectangle)Rectangle to restrict to
A new rectangle restricted to the restrict bounds

Restrict

#
public static Rectangle Restrict(Rectangle rectangle, Size size)
Restricts the rectangle to be within the specified size
Remarks: This ensures that rectangle's bounds fall within the bounds of the specified size It is useful to ensure that the rectangle does not exceed certain limits (e.g. for drawing)
rectangle (Rectangle)Rectangle to restrict
size (Size)Size to restrict to
A new rectangle restricted to the restrict bounds

Restrict

#
public void Restrict(Point location, Size size)
Restricts the rectangle to be within the specified location and size
Remarks: This is a shortcut for Restrict(Rectangle)
location (Point)Minimum location for the rectangle
size (Size)Maximum size for the rectangle

Restrict

#
public void Restrict(Rectangle rectangle)
Restricts the rectangle to be within the specified rectangle
Remarks: This ensures that the current rectangle's bounds fall within the bounds of the specified rectangle. It is useful to ensure that the rectangle does not exceed certain limits (e.g. for drawing)
rectangle (Rectangle)Rectangle to restrict this instance to

Restrict

#
public void Restrict(Size size)
Restricts the rectangle to be within the specified size at an X,Y location of 0, 0

Round

#
public static Rectangle Round(RectangleF rectangle)
Converts a floating point rectangle to an integral Rectangle by rounding the X, Y, Width, and Height.
rectangle (RectangleF)Rectangle to round
A new instance of a Rectangle with rounded X, Y, Width, and Height values

ToString

#
public override string ToString()
Converts this rectangle to a string

Truncate

#
public static Rectangle Truncate(RectangleF rectangle)
Converts a floating point rectangle to an integral Rectangle by truncating the X, Y, Width, and Height values
rectangle (RectangleF)Rectangle to truncate
A new instance of a Rectangle with truncated X, Y, Width, and Height values

Union

#
public static Rectangle Union(Rectangle rect1, Rectangle rect2)
Combines two rectangles into one rectangle that encompasses both
rect1 (Rectangle)First rectangle to union
rect2 (Rectangle)Second rectangle to union
A new Rectangle that encompasses both rect1 and rect2

Union

#
public void Union(Rectangle rectangle)
Union the rectangle into this instance to encompass both rectangles

Operators

OperationResultDescription
Rectangle+PaddingRectangleReturns a rectangle inset by the specified padding
Rectangle+PointRectangleAdds the offset to the specified rectangle, moving its location
Rectangle+SizeRectangleAdds the offset to the specified rectangle, moving its location
Rectangle/intRectangleDivides all X, Y, Width, Height components of the rectangle by a factor factor
Rectangle/SizeRectangleDivides the specified rectangle by the Width and Height of size
Rectangle==RectangleboolCompares two rectangles for equality
Rectangle!=RectangleboolCompares two rectangles for inequality
Rectangle*intRectangleMultiplies all X, Y, Width, Height components of the rectangle by a factor
Rectangle*SizeRectangleMultiplies the specified rectangle by the Width and Height of size
Rectangle-PaddingRectangleReturns a rectangle expanded by the specified padding
Rectangle-PointRectangleSubtracts the offset from the specified rectangle, moving its location
Rectangle-SizeRectangleSubtracts the offset from the specified rectangle, moving its location

Convertors

OperationResultDescription
explicitRectangleFRectangleImplicit conversion from a RectangleF to a Rectangle