Convert CSS Pixels, Centimeters, and Millimeters

The Unit API converts numeric lengths between CSS pixels and absolute units such as centimeters and millimeters. It follows the CSS reference ratio of 96px = 1in, so the same input produces the same result on every supported platform. Use this hub to select a focused C# workflow; the online Pixel Calculator remains available for image and print calculations.


Convert Pixels to Centimeters and Millimeters

Create one pixel Length, then request each required output unit through GetValue().

Convert Pixels to CM and MM in C#

using Aspose.Html.Drawing;

// Create a length value in CSS pixels
Length pixels = Unit.FromPixels(1000);

// Convert the same value to centimeters and millimeters
double centimeters = pixels.GetValue(UnitType.Cm);
double millimeters = pixels.GetValue(UnitType.Mm);

Console.WriteLine($"1000 px = {centimeters:F2} cm");
Console.WriteLine($"1000 px = {millimeters:F2} mm");

The example prints 1000 px = 26.46 cm and 1000 px = 264.58 mm. A single Length can be converted to more than one compatible UnitType without rebuilding the source value.


Choose a Pixel Conversion Workflow

Select the workflow that matches the source value and required result. Each guide contains its own formula, reference values, C# example, and custom-PPI boundary.

ConversionUnit API workflowDetailed guide
PX to CMFromPixels() → UnitType.CmPixels to centimeters
CM to PXFromCentimeters() → UnitType.PxCentimeters to pixels
PX to MMFromPixels() → UnitType.MmPixels to millimeters
MM to PXFromMillimeters() → UnitType.PxMillimeters to pixels

How to Convert CSS Length Units in C#

  1. Import the Aspose.Html.Drawing namespace.
  2. Create a Length with the factory method that matches the source unit, such as Unit.FromPixels() or Unit.FromCentimeters().
  3. Pass the required target value from UnitType to GetValue().
  4. Keep the returned double for further calculations and format it only when presenting the result.

Pixel and CSS Unit Conversion Resources

Read the unit conversion guide for the CSS length model and related examples. Use the API Reference to review the supported members of Unit , Length , and UnitType .


Get Started with Aspose.HTML for .NET API

Install Aspose.HTML for .NET from NuGet with the .NET CLI:

dotnet add package Aspose.HTML

You can also use the NuGet Package Manager UI or run Install-Package Aspose.HTML in the Package Manager Console. See the installation guide for setup details, or request a temporary license to evaluate the API without evaluation limitations.


Frequently Asked Questions

1. Does the Aspose.HTML C# pixel calculator require an HTML document?

The Unit, Length, and UnitType APIs can convert CSS length values without creating or loading an HTMLDocument. They can also be used alongside document layout and rendering code when those workflows need converted dimensions.

2. When should I use the Unit API instead of the online Pixel Calculator?

Use the Unit API for CSS length conversions based on the fixed ratio of 96 pixels per inch. Use the online Pixel Calculator when an image or print workflow requires a selectable PPI such as 150, 300, or 600.

3. Which length units can the Aspose.HTML Unit API convert besides pixels, centimeters, and millimeters?

The Unit API also represents inches, points, picas, and quarter-millimeters. Create the source Length with the matching factory method and pass the required target unit to GetValue().

Explore More Aspose.HTML for .NET Features

Continue with API guides for creating, editing, generating, and converting HTML content.