Rectangle Rectangle Rectangle Rectangle Struct

Definition

Stores a set of four integers that represent the location and size of a rectangle

public value class Rectangle
[System.ComponentModel.TypeConverter(typeof(System.Drawing.RectangleConverter))]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public struct Rectangle
type Rectangle = struct
Public Structure Rectangle
Inheritance
Attributes

Examples

The following example draws a rectangle with its upper-left corner at (10, 10). The rectangle has a width of 100 and a height of 50. The second argument passed to the Pen constructor indicates that the pen width is 5 pixels.

When the rectangle is drawn, the pen is centered on the rectangle's boundary. Because the pen width is 5, the sides of the rectangle are drawn 5 pixels wide, such that 1 pixel is drawn on the boundary itself, 2 pixels are drawn on the inside, and 2 pixels are drawn on the outside. For more details on pen alignment, see How to: Set Pen Width and Alignment.

The following illustration shows the resulting rectangle. The dotted lines show where the rectangle would have been drawn if the pen width had been one pixel. The enlarged view of the upper-left corner of the rectangle shows that the thick black lines are centered on those dotted lines.

Pens

The example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler.

Pen blackPen = new Pen(Color.FromArgb(255, 0, 0, 0), 5);
e.Graphics.DrawRectangle(blackPen, 10, 10, 100, 50);
Dim blackPen As New Pen(Color.FromArgb(255, 0, 0, 0), 5)
e.Graphics.DrawRectangle(blackPen, 10, 10, 100, 50)

Remarks

A rectangle is defined by its Width, Height, and upper-left corner represented by the Location property.

To draw rectangles, you need a Graphics object and a Pen object. The Graphics object provides the DrawRectangle method, and the Pen object stores features of the line, such as color and width. The units the rectangle is drawn in is determined by the PageUnit and PageScale properties of the graphics object used for drawing. The default unit is pixels.

To draw a Rectangle filled with color, you need a Graphics object and an object derived from Brush such as SolidBrush or LinearGradientBrush. The Graphics object provides the FillRectangle method and the Brush object provides the color and fill information.

For more advanced shapes, use a Region object.

Constructors

Rectangle(Point, Size) Rectangle(Point, Size) Rectangle(Point, Size) Rectangle(Point, Size)

Initializes a new instance of the Rectangle class with the specified location and size.

Rectangle(Int32, Int32, Int32, Int32) Rectangle(Int32, Int32, Int32, Int32) Rectangle(Int32, Int32, Int32, Int32) Rectangle(Int32, Int32, Int32, Int32)

Initializes a new instance of the Rectangle class with the specified location and size.

Fields

Empty Empty Empty Empty

Represents a Rectangle structure with its properties left uninitialized.

Properties

Bottom Bottom Bottom Bottom

Gets the y-coordinate that is the sum of the Y and Height property values of this Rectangle structure.

Height Height Height Height

Gets or sets the height of this Rectangle structure.

IsEmpty IsEmpty IsEmpty IsEmpty

Tests whether all numeric properties of this Rectangle have values of zero.

Left Left Left Left

Gets the x-coordinate of the left edge of this Rectangle structure.

Location Location Location Location

Gets or sets the coordinates of the upper-left corner of this Rectangle structure.

Right Right Right Right

Gets the x-coordinate that is the sum of X and Width property values of this Rectangle structure.

Size Size Size Size

Gets or sets the size of this Rectangle.

Top Top Top Top

Gets the y-coordinate of the top edge of this Rectangle structure.

Width Width Width Width

Gets or sets the width of this Rectangle structure.

X X X X

Gets or sets the x-coordinate of the upper-left corner of this Rectangle structure.

Y Y Y Y

Gets or sets the y-coordinate of the upper-left corner of this Rectangle structure.

Methods

Ceiling(RectangleF) Ceiling(RectangleF) Ceiling(RectangleF) Ceiling(RectangleF)

Converts the specified RectangleF structure to a Rectangle structure by rounding the RectangleF values to the next higher integer values.

Contains(Point) Contains(Point) Contains(Point) Contains(Point)

Determines if the specified point is contained within this Rectangle structure.

Contains(Rectangle) Contains(Rectangle) Contains(Rectangle) Contains(Rectangle)

Determines if the rectangular region represented by rect is entirely contained within this Rectangle structure.

Contains(Int32, Int32) Contains(Int32, Int32) Contains(Int32, Int32) Contains(Int32, Int32)

Determines if the specified point is contained within this Rectangle structure.

Equals(Rectangle) Equals(Rectangle) Equals(Rectangle) Equals(Rectangle)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Tests whether obj is a Rectangle structure with the same location and size of this Rectangle structure.

FromLTRB(Int32, Int32, Int32, Int32) FromLTRB(Int32, Int32, Int32, Int32) FromLTRB(Int32, Int32, Int32, Int32) FromLTRB(Int32, Int32, Int32, Int32)

Creates a Rectangle structure with the specified edge locations.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Returns the hash code for this Rectangle structure. For information about the use of hash codes, see GetHashCode() .

Inflate(Size) Inflate(Size) Inflate(Size) Inflate(Size)

Enlarges this Rectangle by the specified amount.

Inflate(Int32, Int32) Inflate(Int32, Int32) Inflate(Int32, Int32) Inflate(Int32, Int32)

Enlarges this Rectangle by the specified amount.

Inflate(Rectangle, Int32, Int32) Inflate(Rectangle, Int32, Int32) Inflate(Rectangle, Int32, Int32) Inflate(Rectangle, Int32, Int32)

Creates and returns an enlarged copy of the specified Rectangle structure. The copy is enlarged by the specified amount. The original Rectangle structure remains unmodified.

Intersect(Rectangle, Rectangle) Intersect(Rectangle, Rectangle) Intersect(Rectangle, Rectangle) Intersect(Rectangle, Rectangle)

Returns a third Rectangle structure that represents the intersection of two other Rectangle structures. If there is no intersection, an empty Rectangle is returned.

Intersect(Rectangle) Intersect(Rectangle) Intersect(Rectangle) Intersect(Rectangle)

Replaces this Rectangle with the intersection of itself and the specified Rectangle.

IntersectsWith(Rectangle) IntersectsWith(Rectangle) IntersectsWith(Rectangle) IntersectsWith(Rectangle)

Determines if this rectangle intersects with rect.

Offset(Point) Offset(Point) Offset(Point) Offset(Point)

Adjusts the location of this rectangle by the specified amount.

Offset(Int32, Int32) Offset(Int32, Int32) Offset(Int32, Int32) Offset(Int32, Int32)

Adjusts the location of this rectangle by the specified amount.

Round(RectangleF) Round(RectangleF) Round(RectangleF) Round(RectangleF)

Converts the specified RectangleF to a Rectangle by rounding the RectangleF values to the nearest integer values.

ToString() ToString() ToString() ToString()

Converts the attributes of this Rectangle to a human-readable string.

Truncate(RectangleF) Truncate(RectangleF) Truncate(RectangleF) Truncate(RectangleF)

Converts the specified RectangleF to a Rectangle by truncating the RectangleF values.

Union(Rectangle, Rectangle) Union(Rectangle, Rectangle) Union(Rectangle, Rectangle) Union(Rectangle, Rectangle)

Gets a Rectangle structure that contains the union of two Rectangle structures.

Operators

Equality(Rectangle, Rectangle) Equality(Rectangle, Rectangle) Equality(Rectangle, Rectangle) Equality(Rectangle, Rectangle)

Tests whether two Rectangle structures have equal location and size.

Inequality(Rectangle, Rectangle) Inequality(Rectangle, Rectangle) Inequality(Rectangle, Rectangle) Inequality(Rectangle, Rectangle)

Tests whether two Rectangle structures differ in location or size.

Applies to