C# 将像素转换为点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/139655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 15:11:31  来源:igfitidea点击:

Convert Pixels to Points

提问by Todd Davis

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?

我需要在 C# 中将像素转换为点。我看过一些关于这个话题的复杂解释,但似乎找不到一个简单的公式。让我们假设一个标准的 96dpi,我如何计算这个转换?

采纳答案by Mark Ransom

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

每英寸72 个点;如果假设每英寸 96 个像素就足够了,那么公式就相当简单:

points = pixels * 72 / 96

点数 = 像素 * 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".

有一种方法可以使用GetDeviceCaps. Microsoft 有一个名为“Developing DPI-Aware Applications”的指南,查找“Creating DPI-Aware Fonts”部分。

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

W3C 将像素测量定义px1英寸的 1/96,而不管您的显示器的实际分辨率如何,因此上述公式应该适用于所有网络工作。

回答by JeeBee

Surely this whole question should be:

当然,整个问题应该是:

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

“如何获取显示器的水平和垂直 PPI(每英寸像素数)?”

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.

一英寸有 72 个点(根据定义,“点”定义为 1/72 英寸,同样,“pica”定义为 1/72 英尺)。有了这两位信息,您可以非常轻松地从 px 转换为 pt 并返回。

回答by Joel Coehoorn

Assuming 96dpi is a hugemistake. 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%).

假设 96dpi 是一个巨大的错误。即使假设是正确的,也可以选择缩放字体。因此,设置为 10pts 的字体实际上可能显示为 12.5pt (125%)。

回答by spotcatbug

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.

System.Drawing.Graphics 具有 DpiX 和 DpiY 属性。DpiX 是水平每英寸像素数。DpiY 是垂直每英寸的像素数。使用这些将点(每英寸 72 点)转换为像素。

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

例如:14 个水平点 = (14 * DpiX) / 72 像素

回答by Hejazi

Try this if your code lies in a form:

如果您的代码位于表单中,请尝试此操作:

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

回答by Ian Boyd

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)
  • 一英寸有 72 个点(这就是一个点,一英寸的 1/72)
  • 在设置为 150dpi 的系统上,每英寸有 150 个像素。
  • 1 in = 72pt = 150px(150dpi 设置)

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

如果要根据像素 (px) 查找点 (pt):

 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

回答by david

Actually it must be

其实应该是

points = pixels * 96 / 72

回答by daniel

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 点的示例:

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.

这里手动输入 213.67 点给出 284 像素。
此处手动输入 213.68 点为 285 像素。

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

(为什么是 0.45?我不知道,但它有效。)

回答by Heind

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

WPF 使用 System.Windows.FontSizeConverter 将点转换为像素。FontSizeConverter 使用 System.Windows.LengthConverter。LengthConverter 使用因子 1.333333333333333333 将点 (p) 转换为像素 (x):x = p * 1.3333333333333333

回答by Bubba Tbone

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

点数 =(像素 / 96)* 72 在标准 XP/Vista/7 机器上(出厂默认值)

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

点数 =(像素 / 72)* 72 在运行 OSX 的标准 Mac 上(出厂默认值)

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

Windows 默认以 96dpi(显示)运行 Macs 默认以 72 dpi(显示)运行

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

72 POSTSCRIPT 点 = 1 英寸 12 POSTSCRIPT 点 = 1 POSTSCRIPT Pica 6 POSTSCRIPT Pica = 72 点 = 1 英寸

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

1 点 = 1?72 英寸 = 25.4?72 毫米 = 0.3527 毫米

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

DPI = 每英寸点数 PPI = 每英寸像素数 LPI = 每英寸行数

More info if using em as measuring

如果使用 em 作为测量的更多信息

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

16px = 1em(普通文本的默认值)8em = 16px * 8 Pixels/16 = em