Search Results for

    Show / Hide Table of Contents

    Class Rectangle

    A universally compatible Rectangle for .NET 7, .NET 6, .NET 5, and .NET Core. As well as compatibility with Windows, NanoServer, IIS, macOS, Mobile, Xamarin, iOS, Android, Google Compute, Azure, AWS, and Linux.

    Works nicely with popular Image Rectangle such as System.Drawing.Rectangle, SkiaSharp.SKRect, SixLabors.ImageSharp.Rectangle, Microsoft.Maui.Graphics.Rect.

    Implicit casting means that using this class to input and output Rectangle from public APIs gives full compatibility to all Rectangle type fully supported by Microsoft.

    Inheritance
    System.Object
    Rectangle
    Namespace: IronSoftware.Drawing
    Assembly: IronSoftware.Drawing.Common.dll
    Syntax
    public class Rectangle : Object

    A cross-platform rectangle record, Rectangle is the single geometry type that bridges System.Drawing.Rectangle, SkiaSharp.SKRect, SixLabors.ImageSharp.Rectangle, and Microsoft.Maui.Graphics.Rect without manual conversion code. Wherever a public IronDrawing API accepts or returns a region of an image, Rectangle is the currency, and implicit cast operators handle the translation to and from every supported framework type automatically.

    Construct a Rectangle with coordinates and dimensions directly, using Rectangle(int x, int y, int width, int height, MeasurementUnits units), or from a Point and a Size pair via Rectangle(Point point, Size size, MeasurementUnits units). The MeasurementUnits property records whether the values are expressed in pixels, inches, or millimetres, and ConvertTo(MeasurementUnits toUnits, int dpi) converts the rectangle to a different unit system at a given DPI, defaulting to 96. The default constructor produces a zero-origin, zero-size rectangle ready for property assignment.

    **Geometry properties** cover every edge and dimension needed for layout and hit-testing work:

    - Coordinates and size: X, Y, Width, Height - Derived edges (read-only): Left, Right, Top, Bottom - Unit metadata: Units

    Contains(int x, int y) tests whether a point falls inside the rectangle, which is useful for click-region checks or crop-boundary validation.

    The implicit cast operators are the practical heart of the class. Assigning a Rectangle to a System.Drawing.RectangleF, SkiaSharp.SKRect, SkiaSharp.SKRectI, Microsoft.Maui.Graphics.Rect, or Microsoft.Maui.Graphics.RectF variable requires no explicit conversion. The reverse direction works equally: any of those types assigned to a Rectangle variable is silently promoted. This symmetry means a method that accepts Rectangle can receive a System.Drawing.Rectangle from a caller who has never heard of IronDrawing, and a method that returns Rectangle can feed a SkiaSharp pipeline without an adapter layer.

    using IronSoftware.Drawing;
    
    // Construct from coordinates; units default to pixels via MeasurementUnits.Pixel
    var crop = new Rectangle(10, 20, 640, 480, MeasurementUnits.Pixel);
    
    // Implicit cast to SkiaSharp without any explicit conversion
    SkiaSharp.SKRect skRect = crop;
    
    // Promote a System.Drawing.Rectangle into IronDrawing seamlessly
    System.Drawing.Rectangle sysCrop = new System.Drawing.Rectangle(0, 0, 320, 240);
    Rectangle ironRect = sysCrop;
    
    // Unit conversion: pixels to inches at 150 DPI
    Rectangle inchRect = ironRect.ConvertTo(MeasurementUnits.Inch, 150);
    
    // Point containment check
    bool hit = crop.Contains(100, 200);

    Explore the IronDrawing get-started guide for installation, the drawing docs for the full type catalogue, and the image cropping example for a practical Rectangle workflow.

    Constructors

    Rectangle()

    Construct a new Rectangle.

    Declaration
    public Rectangle()
    See Also
    Rectangle

    Rectangle(Point, Size, MeasurementUnits)

    Initializes a new instance of the Rectangle struct.

    Declaration
    public Rectangle(Point point, Size size, MeasurementUnits units)
    Parameters
    Type Name Description
    Point point

    The Point which specifies the rectangles point in a two-dimensional plane.

    Size size

    The IronSoftware.Drawing.Rectangle.Size which specifies the rectangles height and width.

    MeasurementUnits units

    The measurement unit of this Rectangle

    See Also
    Rectangle

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

    Construct a new Rectangle.

    Declaration
    public Rectangle(int x, int y, int width, int height, MeasurementUnits units)
    Parameters
    Type Name Description
    System.Int32 x

    The x-coordinate of the upper-left corner of this Rectangle

    System.Int32 y

    The y-coordinate of the upper-left corner of this Rectangle

    System.Int32 width

    The width of this Rectangle

    System.Int32 height

    The height of this Rectangle

    MeasurementUnits units

    The measurement unit of this Rectangle

    See Also
    Rectangle

    Properties

    Bottom

    Gets the y-coordinate of the bottom edge of this Rectangle.

    Declaration
    public int Bottom { get; }
    Property Value
    Type Description
    System.Int32

    Height

    The height of this Rectangle. The default is 0.

    Declaration
    public int Height { get; set; }
    Property Value
    Type Description
    System.Int32

    Left

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

    Declaration
    public int Left { get; }
    Property Value
    Type Description
    System.Int32

    Right

    Gets the x-coordinate of the right edge of this Rectangle.

    Declaration
    public int Right { get; }
    Property Value
    Type Description
    System.Int32

    Top

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

    Declaration
    public int Top { get; }
    Property Value
    Type Description
    System.Int32

    Units

    The measurement unit of this Rectangle. The default is Pixels

    Declaration
    public MeasurementUnits Units { get; set; }
    Property Value
    Type Description
    MeasurementUnits

    Width

    The width of this Rectangle. The default is 0.

    Declaration
    public int Width { get; set; }
    Property Value
    Type Description
    System.Int32

    X

    The x-coordinate of the upper-left corner of this Rectangle. The default is 0.

    Declaration
    public int X { get; set; }
    Property Value
    Type Description
    System.Int32

    Y

    The y-coordinate of the upper-left corner of this Rectangle. The default is 0.

    Declaration
    public int Y { get; set; }
    Property Value
    Type Description
    System.Int32

    Methods

    Contains(Int32, Int32)

    Determines if the specified point is contained within the rectangular region defined by this Rectangle.

    Declaration
    public bool Contains(int x, int y)
    Parameters
    Type Name Description
    System.Int32 x

    The x-coordinate of the given point.

    System.Int32 y

    The y-coordinate of the given point.

    Returns
    Type Description
    System.Boolean

    ConvertTo(MeasurementUnits, Int32)

    Convert this rectangle to the specified units of measurement using the specified DPI

    Declaration
    public Rectangle ConvertTo(MeasurementUnits toUnits, int dpi = 96)
    Parameters
    Type Name Description
    MeasurementUnits toUnits

    Unit of measurement

    System.Int32 dpi

    DPI (Dots per inch) for conversion

    Returns
    Type Description
    Rectangle

    A new rectangle which uses the desired units of measurement

    Exceptions
    Type Condition
    System.NotImplementedException

    Conversion not implemented

    Operators

    Implicit(Rectangle to RectangleF)

    Implicitly casts to RectangleF objects from Rectangle.

    When your .NET Class methods use Rectangle as parameters and return types, you now automatically support RectangleF as well.

    Declaration
    public static implicit operator RectangleF(Rectangle Rectangle)
    Parameters
    Type Name Description
    Rectangle Rectangle

    Rectangle is explicitly cast to a RectangleF.

    Returns
    Type Description
    RectangleF

    Implicit(Rectangle to Rect)

    Implicitly casts to Microsoft.Maui.Graphics.Rect objects from Rectangle.

    When your .NET Class methods use Rectangle as parameters and return types, you now automatically support Microsoft.Maui.Graphics.Rect as well.

    Declaration
    public static implicit operator Rect(Rectangle Rectangle)
    Parameters
    Type Name Description
    Rectangle Rectangle

    Rectangle is explicitly cast to a Microsoft.Maui.Graphics.Rect.

    Returns
    Type Description
    Microsoft.Maui.Graphics.Rect

    Implicit(Rectangle to RectF)

    Implicitly casts to Microsoft.Maui.Graphics.RectF objects from Rectangle.

    When your .NET Class methods use Rectangle as parameters and return types, you now automatically support Microsoft.Maui.Graphics.RectF as well.

    Declaration
    public static implicit operator RectF(Rectangle Rectangle)
    Parameters
    Type Name Description
    Rectangle Rectangle

    Rectangle is explicitly cast to a Microsoft.Maui.Graphics.RectF.

    Returns
    Type Description
    Microsoft.Maui.Graphics.RectF

    Implicit(Rectangle to Rectangle)

    Implicitly casts to SixLabors.ImageSharp.Rectangle objects from Rectangle.

    When your .NET Class methods use Rectangle as parameters and return types, you now automatically support SixLabors.ImageSharp.Rectangle as well.

    Declaration
    public static implicit operator Rectangle(Rectangle Rectangle)
    Parameters
    Type Name Description
    Rectangle Rectangle

    Rectangle is explicitly cast to a SixLabors.ImageSharp.Rectangle.

    Returns
    Type Description
    SixLabors.ImageSharp.Rectangle

    Implicit(Rectangle to RectangleF)

    Implicitly casts to SixLabors.ImageSharp.RectangleF objects from Rectangle.

    When your .NET Class methods use Rectangle as parameters and return types, you now automatically support SixLabors.ImageSharp.RectangleF as well.

    Declaration
    public static implicit operator RectangleF(Rectangle Rectangle)
    Parameters
    Type Name Description
    Rectangle Rectangle

    Rectangle is explicitly cast to a SixLabors.ImageSharp.RectangleF.

    Returns
    Type Description
    SixLabors.ImageSharp.RectangleF

    Implicit(Rectangle to SKRect)

    Implicitly casts to SkiaSharp.SKRect objects from Rectangle.

    When your .NET Class methods use Rectangle as parameters and return types, you now automatically support SkiaSharp.SKRect as well.

    Declaration
    public static implicit operator SKRect(Rectangle Rectangle)
    Parameters
    Type Name Description
    Rectangle Rectangle

    Rectangle is explicitly cast to a SkiaSharp.SKRect.

    Returns
    Type Description
    SkiaSharp.SKRect

    Implicit(Rectangle to SKRectI)

    Implicitly casts to SkiaSharp.SKRectI objects from Rectangle.

    When your .NET Class methods use Rectangle as parameters and return types, you now automatically support SkiaSharp.SKRectI as well.

    Declaration
    public static implicit operator SKRectI(Rectangle Rectangle)
    Parameters
    Type Name Description
    Rectangle Rectangle

    Rectangle is explicitly cast to a SkiaSharp.SKRectI.

    Returns
    Type Description
    SkiaSharp.SKRectI

    Implicit(Rectangle to Rectangle)

    Implicitly casts to System.Drawing.Rectangle objects from Rectangle.

    When your .NET Class methods use Rectangle as parameters and return types, you now automatically support Rectangle as well.

    Declaration
    public static implicit operator Rectangle(Rectangle Rectangle)
    Parameters
    Type Name Description
    Rectangle Rectangle

    Rectangle is explicitly cast to a System.Drawing.Rectangle.

    Returns
    Type Description
    System.Drawing.Rectangle

    Implicit(Rect to Rectangle)

    Implicitly casts Microsoft.Maui.Graphics.Rect objects to Rectangle.

    When your .NET Class methods use Rectangle as parameters and return types, you now automatically support Microsoft.Maui.Graphics.Rect as well.

    Declaration
    public static implicit operator Rectangle(Rect rectangle)
    Parameters
    Type Name Description
    Microsoft.Maui.Graphics.Rect rectangle

    Microsoft.Maui.Graphics.Rect will automatically be casted to Rectangle.

    Returns
    Type Description
    Rectangle

    Implicit(RectF to Rectangle)

    Implicitly casts Microsoft.Maui.Graphics.RectF objects to Rectangle.

    When your .NET Class methods use Rectangle as parameters and return types, you now automatically support Microsoft.Maui.Graphics.RectF as well.

    Declaration
    public static implicit operator Rectangle(RectF rectangle)
    Parameters
    Type Name Description
    Microsoft.Maui.Graphics.RectF rectangle

    Microsoft.Maui.Graphics.RectF will automatically be casted to Rectangle.

    Returns
    Type Description
    Rectangle

    Implicit(Rectangle to Rectangle)

    Implicitly casts SixLabors.ImageSharp.Rectangle objects to Rectangle.

    When your .NET Class methods use Rectangle as parameters and return types, you now automatically support SixLabors.ImageSharp.Rectangle as well.

    Declaration
    public static implicit operator Rectangle(Rectangle rectangle)
    Parameters
    Type Name Description
    SixLabors.ImageSharp.Rectangle rectangle

    SixLabors.ImageSharp.Rectangle will automatically be casted to Rectangle.

    Returns
    Type Description
    Rectangle

    Implicit(RectangleF to Rectangle)

    Implicitly casts SixLabors.ImageSharp.RectangleF objects to Rectangle.

    When your .NET Class methods use Rectangle as parameters and return types, you now automatically support SixLabors.ImageSharp.RectangleF as well.

    Declaration
    public static implicit operator Rectangle(RectangleF rectangle)
    Parameters
    Type Name Description
    SixLabors.ImageSharp.RectangleF rectangle

    SixLabors.ImageSharp.RectangleF will automatically be casted to Rectangle.

    Returns
    Type Description
    Rectangle

    Implicit(SKRect to Rectangle)

    Implicitly casts SkiaSharp.SKRect objects to Rectangle.

    When your .NET Class methods use Rectangle as parameters and return types, you now automatically support SkiaSharp.SKRect as well.

    Declaration
    public static implicit operator Rectangle(SKRect sKRect)
    Parameters
    Type Name Description
    SkiaSharp.SKRect sKRect

    SkiaSharp.SKRect will automatically be casted to Rectangle.

    Returns
    Type Description
    Rectangle

    Implicit(SKRectI to Rectangle)

    Implicitly casts SkiaSharp.SKRectI objects to Rectangle.

    When your .NET Class methods use Rectangle as parameters and return types, you now automatically support SkiaSharp.SKRectI as well.

    Declaration
    public static implicit operator Rectangle(SKRectI sKRectI)
    Parameters
    Type Name Description
    SkiaSharp.SKRectI sKRectI

    SkiaSharp.SKRectI will automatically be casted to Rectangle.

    Returns
    Type Description
    Rectangle

    Implicit(Rectangle to Rectangle)

    Implicitly casts System.Drawing.Rectangle objects to Rectangle.

    When your .NET Class methods use Rectangle as parameters and return types, you now automatically support Rectangle as well.

    Declaration
    public static implicit operator Rectangle(Rectangle rectangle)
    Parameters
    Type Name Description
    System.Drawing.Rectangle rectangle

    System.Drawing.Rectangle will automatically be casted to Rectangle.

    Returns
    Type Description
    Rectangle
    ☀
    ☾
    Downloads
    • Download with Nuget
    In This Article
    Back to top
    Install with Nuget
    IronDrawing_for_dotnet_log2o
    Blue key in circleGet started for FREE
    No credit card required
    Test in a live environment

    Test in production without watermarks.
    Works wherever you need it to.

    Fully-functional product

    Get 30 days of fully functional product.
    Have it up and running in minutes.

    24/5 technical support

    Full access to our support engineering team during your product trial

    Grey key in circleGet started for FREE
    The trial form was submitted successfully.
    Calendar in circleBook Free Live Demo
    No contact, no card details, no commitments Book a 30-minute, personal demo.
    Here's what to expect:

    A live demo of our product and its key features

    Get project specific feature recommendations

    All your questions are answered to make sure you have all the information you need. (No commitment whatsoever.)

    Grey key in circleBook Free Live Demo
    Your booking has been completed Check your e-mail for confirmation
    Support Team Member 6 related to The C# PDF Library Support Team Member 14 related to The C# PDF Library Support Team Member 4 related to The C# PDF Library Support Team Member 2 related to The C# PDF Library
    Online 24/5
    Need help? Our sales team would be glad to help you.
    Try the Enterprise Trial
    ironpdf_for_dotnet_log2o
    Key in blue circle
    Get your free 30-day Trial Key instantly.
    bullet_checkedNo credit card or account creation required
    Key in blue circle
    Get your free 30-day Trial Key instantly.
    Blue key in circleNo credit card or account creation required
    Green Check in orange circle
    The trial form was submitted successfully.
    badge_greencheck_in_yellowcircle
    Thank you for starting a trial

    Please check your email for the trial license key.

    If you don’t receive an email, please start a live chat or email support@ironsoftware.com

    Install with NuGet
    View Licensing
    • Logo Aetna
    • Logo NASA
    • Logo GE
    • Logo Porsche
    • Logo USDA
    • Logo Qatar
    Join Millions of Engineers who’ve tried IronDrawing