Convert Pixels to Points
Asked Answered
S

12

137

I have a need to convert Pixels to Points in C#. I've seen some complicated explanations about the topic, but can't seem to locate a simple formula. Let's assume a standard 96dpi, how do I calulate this conversion?

Squalor answered 26/9, 2008 at 13:57 Comment(0)
K
222

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.

Kanishakanji answered 26/9, 2008 at 14:4 Comment(5)
72 Points Per inch is NOT an arguable item, it is the same across the board!Acyl
@David dpi is dependent on the display, ppi is a typographic constantReluct
@David that article just explains why monitors have variable DPI. PPI is still the same. The number of kilometers in a mile doesn't change depending on the car you drive. Assuming 96dpi is generally not a good idea, but if we do that then the given formula is correct.Piselli
Usually PPI is the abbreviation for pixels per inch, not points per inch. Perhaps that's where the confusion comes from? Points per inch are always 72; thanks for the link Xetius.Kanishakanji
@Corey Ross: Actually, DPI would be dependent on the printer, while PPI would be pendent of the monitor. But everybody wrongly calls PPI DPI...Hyperactive
Q
58

Try this if your code lies in a form:

Graphics g = this.CreateGraphics();
points = pixels * 72 / g.DpiX;
g.Dispose();
Queeniequeenly answered 3/7, 2009 at 12:6 Comment(1)
Is it worth pointing out that DpiX is for Width where DpiY should be used for height?Goods
P
25

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%).

Paniagua answered 26/9, 2008 at 14:6 Comment(0)
L
25

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
Lazuli answered 10/8, 2009 at 17:55 Comment(1)
dpi in the first equation should be px - for cancelling of terms, since we have the assumption (1) for 150dpi systemSkinned
W
8

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

Wootan answered 2/9, 2010 at 18:44 Comment(1)
Please also explain why this value is used... Coincidentally, it is 96 dpi (default DPI of windows for displays) / 72 dpi (DPI that points are defined at).Fernyak
S
4

System.Drawing.Graphics has DpiX and DpiY properties. DpiX is pixels per inch horizontally. DpiY is pixels per inch vertically. Use those to convert from points (72 points per inch) to pixels.

Ex: 14 horizontal points = (14 * DpiX) / 72 pixels

Spleeny answered 26/9, 2008 at 14:45 Comment(0)
H
3

Surely this whole question should be:

"How do I obtain the horizontal and vertical PPI (Pixels Per Inch) of the monitor?"

There are 72 points in an inch (by definition, a "point" is defined as 1/72nd of an inch, likewise a "pica" is defined as 1/72nd of a foot). With these two bits of information you can convert from px to pt and back very easily.

Hass answered 26/9, 2008 at 13:59 Comment(4)
To make it even more complicated, I'm dealing with aligning things on a an Reporting Services (RDL) report which is being converted into a PDF. At the end of the day, who the heck knows what the DPI is? I'm using my best guess. :)Squalor
You mean "How do I obtain the horizontal and vertical DPI of the monitor?". PPI is a constant of 72. Always has been and always will.Bromoform
Pixels Per Inch, not Points Per Inch (Pica).Hass
Um, oops, not Pica! That's 1/6th of an inch. Points Per Inch is redundant, the term is "Points". A "Point" is 1/72nd of an Inch.Hass
T
3

Actually it must be

points = pixels * 96 / 72
Tartaric answered 12/7, 2010 at 12:2 Comment(1)
This is incorrect if you are presuming 96dpi, and 72ppi (points per inch). It will be points = pixels * 72 / 96Skinned
A
3

points = (pixels / 96) * 72 on a standard XP/Vista/7 machine (factory defaults)

points = (pixels / 72) * 72 on a standard Mac running OSX (Factory defaults)

Windows runs as default at 96dpi (display) Macs run as default at 72 dpi (display)

72 POSTSCRIPT Points = 1 inch 12 POSTSCRIPT Points = 1 POSTSCRIPT Pica 6 POSTSCRIPT Picas = 72 Points = 1 inch

1 point = 1⁄72 inches = 25.4⁄72 mm = 0.3527 mm

DPI = Dots Per Inch PPI = Pixels Per Inch LPI = Lines per inch

More info if using em as measuring

16px = 1em (default for normal text) 8em = 16px * 8 Pixels/16 = em

Albuminuria answered 25/8, 2011 at 20:55 Comment(0)
I
1

This works:

int pixels = (int)((dp) * Resources.System.DisplayMetrics.Density + 0.5f);
Inexpert answered 7/8, 2016 at 5:12 Comment(0)
H
0

Using wxPython on Mac to get the correct DPI as follows:

    from wx import ScreenDC
    from wx import Size

    size: Size = ScreenDC().GetPPI()
    print(f'x-DPI: {size.GetWidth()} y-DPI: {size.GetHeight()}')

This yields:

x-DPI: 72 y-DPI: 72

Thus, the formula is:

points: int = (pixelNumber * 72) // 72

Hyperemia answered 8/7, 2020 at 19:26 Comment(0)
E
-2

Height lines converted into points and pixel (my own formula). Here is an example with a manual entry of 213.67 points in the Row Height field:

213.67  Manual Entry    
  0.45  Add 0.45    
214.12  Subtotal    
213.75  Round to a multiple of 0.75 
213.00  Subtract 0.75 provides manual entry converted by Excel  
284.00  Divide by 0.75 gives the number of pixels of height

Here the manual entry of 213.67 points gives 284 pixels.
Here the manual entry of 213.68 points gives 285 pixels.

(Why 0.45? I do not know but it works.)

Esculent answered 16/8, 2010 at 22:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.