Convert Measurement Units
using IronSoftware.Drawing;
using System;

// Instantiate a Rectangle (Default measurement unit is pixels)
Rectangle pxCropRect = new Rectangle(15, 25, 150, 175); // (X, Y, Width, Height)

// Convert and print a Rectangle measurement in millimeters
Rectangle mmCropRect = pxCropRect.ConvertTo(MeasurementUnits.Millimeters, 20);
Console.WriteLine(mmCropRect.X);

// Instantiate a RectangleF in millimeters
IronSoftware.Drawing.RectangleF pxCropRectFloat = new IronSoftware.Drawing.RectangleF(10F, 25F, 100F, 175F, MeasurementUnits.Millimeters); // (X, Y, Width, Height, Units)

// Convert and print a RectangleF measurement in pixels
IronSoftware.Drawing.RectangleF mmCropRectFloat = pxCropRectFloat.ConvertTo(MeasurementUnits.Pixels, 30);
Console.WriteLine(mmCropRectFloat.Y);


NuGet Download
PM > Install-Package IronSoftware.System.Drawing
Convert Measurement Units

Convert Measurement Units

IronDrawing provides an option for users who want to change the measurement unit used in Rectangle or RectangleF from pixels (px) to millimeters (mm) or vice versa. The default measurement unit used in Rectangle and RectangleF is pixels (px).

After a new Rectangle with measurements specified has been instantiated, you can call the conversion method on the Rectangle. For example, use ConvertTo(MeasurementUnits.Millimeters, 20). This will convert the measurements of the Rectangle to millimeters based on the DPI set by the user. This same approach can also be applied to the RectangleF class.

View on GitHub

Ready to Get Started?

Nuget Downloads 21,390,103Version:2026.11just released