Rectangle & RectangleF

IronDrawing provides two classes that allow you to draw a rectangle at particular coordinates: IronSoftware.Drawing.Rectangle and IronSoftware.Drawing.RectangleF. These classes contain a set of four parameters that represent the location and size of the rectangle drawn.

These classes take x and y coordinates from the top-left corner of the rectangle. The Rectangle class uses int types for these coordinates, while the RectangleF class uses float types. The last two parameters represent the width and height of the rectangle, using the same data types as indicated for the x and y coordinate parameters. The default unit for both classes is pixels.

Here’s an example of how to use these classes in C# to create and draw a rectangle.

// Import necessary namespaces
using System;
using IronSoftware.Drawing;

class RectangleExample
{
    static void Main()
    {
        // Create a Rectangle object with x, y, width, and height
        // Parameters are int types for the Rectangle class
        Rectangle rect = new Rectangle(10, 20, 100, 50);

        // Output the Rectangle properties
        Console.WriteLine("Rectangle Properties:");
        Console.WriteLine($"X: {rect.X}, Y: {rect.Y}, Width: {rect.Width}, Height: {rect.Height}");

        // Create a RectangleF object with x, y, width, and height
        // Parameters are float types for the RectangleF class
        RectangleF rectF = new RectangleF(15.5f, 25.5f, 150.5f, 75.5f);

        // Output the RectangleF properties
        Console.WriteLine("RectangleF Properties:");
        Console.WriteLine($"X: {rectF.X}, Y: {rectF.Y}, Width: {rectF.Width}, Height: {rectF.Height}");
    }
}
// Import necessary namespaces
using System;
using IronSoftware.Drawing;

class RectangleExample
{
    static void Main()
    {
        // Create a Rectangle object with x, y, width, and height
        // Parameters are int types for the Rectangle class
        Rectangle rect = new Rectangle(10, 20, 100, 50);

        // Output the Rectangle properties
        Console.WriteLine("Rectangle Properties:");
        Console.WriteLine($"X: {rect.X}, Y: {rect.Y}, Width: {rect.Width}, Height: {rect.Height}");

        // Create a RectangleF object with x, y, width, and height
        // Parameters are float types for the RectangleF class
        RectangleF rectF = new RectangleF(15.5f, 25.5f, 150.5f, 75.5f);

        // Output the RectangleF properties
        Console.WriteLine("RectangleF Properties:");
        Console.WriteLine($"X: {rectF.X}, Y: {rectF.Y}, Width: {rectF.Width}, Height: {rectF.Height}");
    }
}
' Import necessary namespaces
Imports System
Imports IronSoftware.Drawing

Friend Class RectangleExample
	Shared Sub Main()
		' Create a Rectangle object with x, y, width, and height
		' Parameters are int types for the Rectangle class
		Dim rect As New Rectangle(10, 20, 100, 50)

		' Output the Rectangle properties
		Console.WriteLine("Rectangle Properties:")
		Console.WriteLine($"X: {rect.X}, Y: {rect.Y}, Width: {rect.Width}, Height: {rect.Height}")

		' Create a RectangleF object with x, y, width, and height
		' Parameters are float types for the RectangleF class
		Dim rectF As New RectangleF(15.5F, 25.5F, 150.5F, 75.5F)

		' Output the RectangleF properties
		Console.WriteLine("RectangleF Properties:")
		Console.WriteLine($"X: {rectF.X}, Y: {rectF.Y}, Width: {rectF.Width}, Height: {rectF.Height}")
	End Sub
End Class
$vbLabelText   $csharpLabel
  • This example demonstrates the creation of a Rectangle object using integer parameters and a RectangleF object using float parameters.
  • The Console.WriteLine statements are used to output the properties of each rectangle, demonstrating the position (x, y) and size (width, height) of the rectangle.