1 out of 1 rated this helpful Rate this topic

Rectangle Structure

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

Namespace:  System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)
[SerializableAttribute]
[TypeConverterAttribute(typeof(RectangleConverter))]
[ComVisibleAttribute(true)]
public struct Rectangle

The Rectangle type exposes the following members.

  Name Description
Public method Rectangle(Point, Size) Initializes a new instance of the Rectangle class with the specified location and size.
Public method Supported by the XNA Framework Rectangle(Int32, Int32, Int32, Int32) Initializes a new instance of the Rectangle class with the specified location and size.
Top
  Name Description
Public property Supported by the XNA Framework Bottom Gets the y-coordinate that is the sum of the Y and Height property values of this Rectangle structure.
Public property Supported by the XNA Framework Height Gets or sets the height of this Rectangle structure.
Public property Supported by the XNA Framework IsEmpty Tests whether all numeric properties of this Rectangle have values of zero.
Public property Supported by the XNA Framework Left Gets the x-coordinate of the left edge of this Rectangle structure.
Public property Location Gets or sets the coordinates of the upper-left corner of this Rectangle structure.
Public property Supported by the XNA Framework Right Gets the x-coordinate that is the sum of X and Width property values of this Rectangle structure.
Public property Size Gets or sets the size of this Rectangle.
Public property Supported by the XNA Framework Top Gets the y-coordinate of the top edge of this Rectangle structure.
Public property Supported by the XNA Framework Width Gets or sets the width of this Rectangle structure.
Public property Supported by the XNA Framework X Gets or sets the x-coordinate of the upper-left corner of this Rectangle structure.
Public property Supported by the XNA Framework Y Gets or sets the y-coordinate of the upper-left corner of this Rectangle structure.
Top
  Name Description
Public method Static member Ceiling Converts the specified RectangleF structure to a Rectangle structure by rounding the RectangleF values to the next higher integer values.
Public method Contains(Point) Determines if the specified point is contained within this Rectangle structure.
Public method Supported by the XNA Framework Contains(Rectangle) Determines if the rectangular region represented by rect is entirely contained within this Rectangle structure.
Public method Supported by the XNA Framework Contains(Int32, Int32) Determines if the specified point is contained within this Rectangle structure.
Public method Supported by the XNA Framework Equals Tests whether obj is a Rectangle structure with the same location and size of this Rectangle structure. (Overrides ValueType.Equals(Object).)
Protected method Supported by the XNA Framework Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Static member Supported by the XNA Framework FromLTRB Creates a Rectangle structure with the specified edge locations.
Public method Supported by the XNA Framework GetHashCode Returns the hash code for this Rectangle structure. For information about the use of hash codes, see GetHashCode . (Overrides ValueType.GetHashCode.)
Public method Supported by the XNA Framework GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Inflate(Size) Enlarges this Rectangle by the specified amount.
Public method Supported by the XNA Framework Inflate(Int32, Int32) Enlarges this Rectangle by the specified amount.
Public method Static member 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.
Public method Supported by the XNA Framework Intersect(Rectangle) Replaces this Rectangle with the intersection of itself and the specified Rectangle.
Public method Static member Supported by the XNA Framework 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.
Public method Supported by the XNA Framework IntersectsWith Determines if this rectangle intersects with rect.
Protected method Supported by the XNA Framework MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Offset(Point) Adjusts the location of this rectangle by the specified amount.
Public method Supported by the XNA Framework Offset(Int32, Int32) Adjusts the location of this rectangle by the specified amount.
Public method Static member Round Converts the specified RectangleF to a Rectangle by rounding the RectangleF values to the nearest integer values.
Public method Supported by the XNA Framework ToString Converts the attributes of this Rectangle to a human-readable string. (Overrides ValueType.ToString.)

In XNA Framework 3.0, this member is inherited from Object.ToString.
Public method Static member Truncate Converts the specified RectangleF to a Rectangle by truncating the RectangleF values.
Public method Static member Supported by the XNA Framework Union Gets a Rectangle structure that contains the union of two Rectangle structures.
Top
  Name Description
Public operator Static member Supported by the XNA Framework Equality Tests whether two Rectangle structures have equal location and size.
Public operator Static member Supported by the XNA Framework Inequality Tests whether two Rectangle structures differ in location or size.
Top
  Name Description
Public field Static member Supported by the XNA Framework Empty Represents a Rectangle structure with its properties left uninitialized.
Top

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.

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

This example is designed for use with Windows Forms, and it requires PaintEventArgs e, 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);


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ