Create and Cast Rectangle

Rectangle and RectangleF are useful features provided by IronDrawing which can be used to crop any image file. Customers using rectangles from System.Drawing.Rectangle or System.Drawing.RectangleF can also cast them to IronSoftware.Drawing.Rectangle and IronSoftware.Drawing.RectangleF using the Cast method.

Create Rectangle & RectangleF

To create a Rectangle, users can instantiate a new Rectangle and provide the X and Y coordinates, as well as the width and height measurement of the Rectangle in pixels. For example:

// Create a Rectangle with specific coordinates and size
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(10, 10, 50, 50);
// Create a Rectangle with specific coordinates and size
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(10, 10, 50, 50);
' Create a Rectangle with specific coordinates and size
Dim rectangle As New System.Drawing.Rectangle(10, 10, 50, 50)
$vbLabelText   $csharpLabel

Similarly, for the RectangleF class, a sample RectangleF object can be instantiated as follows:

// Create a RectangleF with floating point coordinates and size
System.Drawing.RectangleF rectangleF = new System.Drawing.RectangleF(10.2F, 16.5F, 150F, 60F);
// Create a RectangleF with floating point coordinates and size
System.Drawing.RectangleF rectangleF = new System.Drawing.RectangleF(10.2F, 16.5F, 150F, 60F);
' Create a RectangleF with floating point coordinates and size
Dim rectangleF As New System.Drawing.RectangleF(10.2F, 16.5F, 150F, 60F)
$vbLabelText   $csharpLabel

Cast Rectangle & RectangleF

To cast an IronSoftware.Drawing.Rectangle from System.Drawing.Rectangle or an IronSoftware.Drawing.RectangleF from System.Drawing.RectangleF, you can use implicit conversion. Here's how you can do it:

// Assume existing System.Drawing.Rectangle and System.Drawing.RectangleF
System.Drawing.Rectangle systemRect = new System.Drawing.Rectangle(0, 0, 100, 50);
System.Drawing.RectangleF systemRectF = new System.Drawing.RectangleF(0.5F, 0.5F, 100.5F, 50.5F);

// Casting to IronSoftware.Drawing types
IronSoftware.Drawing.Rectangle ironRect = systemRect; // Implicit conversion
IronSoftware.Drawing.RectangleF ironRectF = systemRectF; // Implicit conversion
// Assume existing System.Drawing.Rectangle and System.Drawing.RectangleF
System.Drawing.Rectangle systemRect = new System.Drawing.Rectangle(0, 0, 100, 50);
System.Drawing.RectangleF systemRectF = new System.Drawing.RectangleF(0.5F, 0.5F, 100.5F, 50.5F);

// Casting to IronSoftware.Drawing types
IronSoftware.Drawing.Rectangle ironRect = systemRect; // Implicit conversion
IronSoftware.Drawing.RectangleF ironRectF = systemRectF; // Implicit conversion
' Assume existing System.Drawing.Rectangle and System.Drawing.RectangleF
Dim systemRect As New System.Drawing.Rectangle(0, 0, 100, 50)
Dim systemRectF As New System.Drawing.RectangleF(0.5F, 0.5F, 100.5F, 50.5F)

' Casting to IronSoftware.Drawing types
Dim ironRect As IronSoftware.Drawing.Rectangle = systemRect ' Implicit conversion
Dim ironRectF As IronSoftware.Drawing.RectangleF = systemRectF ' Implicit conversion
$vbLabelText   $csharpLabel

In this example, we use implicit conversion to cast System.Drawing.Rectangle and System.Drawing.RectangleF to their IronSoftware.Drawing counterparts, making it straightforward to utilize these rectangles in IronDrawing's features.