Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Pixels to Points

Tags:

c#

.net

pixel

point

People also ask

How many pixels is a point?

A point (pt) is equal to 0.352778 millimeters, 0.0138889 inches, or 1.333 pixels.

How many pixels is a 12 point font?

Font size specifications may come in points or pixels where: 1 pixel (px) is usually assumed to be 1/96th of an inch. 1 point (pt) is assumed to be 1/72nd of an inch. Therefore 16px = 12pt.

Is pixels the same as points?

Pixels and points are static measurements - they don't change based on other factors: 1 pixel is always 1 pixel and is the smallest piece of a screen that can display colours. 1 point is always 1 point and is an abstract unit, only having meaning when referenced in relation to other points.


There are 72 points per inch; if it is sufficient to assume 96 pixels per inch, the formula is rather simple:

points = pixels * 72 / 96

There is a way to get the configured pixels per inch of your display in Windows using GetDeviceCaps. Microsoft has a guide called "Developing DPI-Aware Applications", look for the section "Creating DPI-Aware Fonts".

The W3C has defined the pixel measurement px as exactly 1/96th of 1in regardless of the actual resolution of your display, so the above formula should be good for all web work.


Try this if your code lies in a form:

Graphics g = this.CreateGraphics();
points = pixels * 72 / g.DpiX;
g.Dispose();

Starting with the given:

  • There are 72 points in an inch (that is what a point is, 1/72 of an inch)
  • on a system set for 150dpi, there are 150 pixels per inch.
  • 1 in = 72pt = 150px (for 150dpi setting)

If you want to find points (pt) based on pixels (px):

 72 pt    x pt
------ = -----                  (1) for 150dpi system
150 px    y px

Rearranging:

x = (y/150) * 72                (2) for 150dpi system

so:

points = (pixels / 150) * 72    (3) for 150dpi system

Assuming 96dpi is a huge mistake. Even if the assumption is right, there's also an option to scale fonts. So a font set for 10pts may actually be shown as if it's 12.5pt (125%).


WPF converts points to pixels with the System.Windows.FontSizeConverter. The FontSizeConverter uses the System.Windows.LengthConverter. The LengthConverter uses the factor 1.333333333333333333 to convert from points (p) to pixels (x): x = p * 1.3333333333333333