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?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Try this if your code lies in a form:
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.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%).
Starting with the given:
If you want to find points (pt) based on pixels (px):
Rearranging:
so:
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
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:
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.)