What is Rectangle width and height unit ? is it in pixels?
Categories:
Understanding Rectangle Width and Height Units in C#

Explore the default units for Rectangle dimensions in C# graphics, focusing on pixels, and how different graphics contexts can influence interpretation.
When working with graphics in C#, particularly with the System.Drawing.Rectangle
or System.Windows.Rect
(WPF) structures, a common question arises: what unit of measurement do the Width
and Height
properties represent? The answer, in most standard C# graphics contexts, is pixels. However, understanding the nuances of different graphics environments is crucial for accurate rendering.
Pixels: The Default Unit
In the context of GDI+ (Graphics Device Interface Plus) used in Windows Forms applications, and generally when drawing directly onto a Graphics
object, the Width
and Height
of a Rectangle
are indeed measured in pixels. A pixel is the smallest addressable element on a display device. This means that a Rectangle
with Width = 100
and Height = 50
will occupy 100 pixels horizontally and 50 pixels vertically on the screen or drawing surface.
using System.Drawing;
using System.Windows.Forms;
public class MyForm : Form
{
public MyForm()
{
this.Text = "Rectangle Pixel Example";
this.Paint += new PaintEventHandler(MyForm_Paint);
}
private void MyForm_Paint(object sender, PaintEventArgs e)
{
// Create a red pen
using (Pen redPen = new Pen(Color.Red, 2))
{
// Define a rectangle with width 100 and height 50 pixels
Rectangle rect = new Rectangle(50, 50, 100, 50);
e.Graphics.DrawRectangle(redPen, rect);
}
}
public static void Main()
{
Application.Run(new MyForm());
}
}
C# Windows Forms example demonstrating a Rectangle drawn with pixel units.
DPI and Device-Independent Units (WPF)
While pixels are the default for GDI+, modern UI frameworks like Windows Presentation Foundation (WPF) introduce the concept of device-independent units. In WPF, System.Windows.Rect
dimensions are typically measured in 1/96th of an inch. This means that a Rect
with Width = 96
will always be one inch wide, regardless of the screen's DPI (Dots Per Inch) setting. The WPF rendering engine then scales these device-independent units to actual pixels based on the system's DPI. This approach helps applications look consistent across different displays and DPI settings.
flowchart TD A[Rectangle Definition (C#)] --> B{Graphics Context?} B -- GDI+ (WinForms) --> C[Units: Pixels] B -- WPF --> D[Units: Device-Independent (1/96th inch)] C --> E[Direct Pixel Mapping] D --> F[DPI Scaling] F --> E
Flowchart illustrating how Rectangle units are interpreted based on the graphics context.
Coordinate Systems and Transformations
Beyond the base unit, it's important to consider the coordinate system and any transformations applied to the Graphics
object. A Graphics
object has a PageUnit
property that can be set to values like Display
, Document
, Inch
, Millimeter
, Point
, or Pixel
. While Pixel
is the default, if you change this property, your Rectangle
dimensions will be interpreted according to the new unit. Similarly, transformations like scaling, rotation, or translation applied via e.Graphics.ScaleTransform()
, e.Graphics.RotateTransform()
, or e.Graphics.TranslateTransform()
will affect how the Rectangle
is rendered on the final output device.
using System.Drawing;
using System.Windows.Forms;
public class MyFormScaled : Form
{
public MyFormScaled()
{
this.Text = "Rectangle Scaled Example";
this.Paint += new PaintEventHandler(MyFormScaled_Paint);
}
private void MyFormScaled_Paint(object sender, PaintEventArgs e)
{
// Set the page unit to inches
e.Graphics.PageUnit = GraphicsUnit.Inch;
using (Pen bluePen = new Pen(Color.Blue, 0.05f)) // Pen width in inches
{
// Define a rectangle with width 1 inch and height 0.5 inch
RectangleF rect = new RectangleF(0.5f, 0.5f, 1.0f, 0.5f);
e.Graphics.DrawRectangle(bluePen, rect.X, rect.Y, rect.Width, rect.Height);
}
// Reset to default for other drawings if needed
e.Graphics.PageUnit = GraphicsUnit.Pixel;
}
public static void Main()
{
Application.Run(new MyFormScaled());
}
}
C# Windows Forms example demonstrating a Rectangle drawn with GraphicsUnit.Inch
.
Graphics
object's PageUnit
and any applied transformations.