在 C# 中将像素转换为英寸,反之亦然
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/474810/
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
Convert Pixels to Inches and vice versa in C#
提问by AngryHacker
I am looking to convert pixes to inches and vice versa. I understand that I need DPI, but I am not sure how to get this information (e.g. I don't have the Graphics
object, so that's not an option).
我希望将像素转换为英寸,反之亦然。我知道我需要 DPI,但我不确定如何获取此信息(例如,我没有Graphics
对象,所以这不是一个选项)。
Is there a way?
有办法吗?
采纳答案by codekaizen
On a video device, any answer to this question is typically not very accurate. The easiest example to use to see why this is the case is a projector. The output resolution is, say, 1024x768, but the DPI varies by how far away the screen is from the projector apeture. WPF, for example, always assumes 96 DPI on a video device.
在视频设备上,这个问题的任何答案通常都不是很准确。用于了解为什么会出现这种情况的最简单示例是投影仪。输出分辨率为 1024x768,但 DPI 因屏幕与投影仪光圈的距离而异。例如,WPF 在视频设备上始终假定为 96 DPI。
Presuming you still need an answer, regardless of the accuracy, and you don't have a Graphics object, you can create one from the screen with some P/Invoke and get the answer from it.
假设您仍然需要一个答案,无论准确性如何,并且您没有 Graphics 对象,您可以使用一些 P/Invoke 从屏幕创建一个并从中获得答案。
Single xDpi, yDpi;
IntPtr dc = GetDC(IntPtr.Zero);
using(Graphics g = Graphics.FromHdc(dc))
{
xDpi = g.DpiX;
yDpi = g.DpiY;
}
if (ReleaseDC(IntPtr.Zero) != 0)
{
// GetLastError and handle...
}
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
private static extern Int32 ReleaseDC(IntPtr hwnd);
回答by falstro
There's, physically, no real way without knowing the DPI. Pixels are discrete, inches are not, if you're talking inches on your monitor, you need to know (at the very least) the resolution (and pixel aspect ratio) and the size of the visible monitor area in order to calculate your DPI. The resolution is usually possible to fetch somewhere (I'm not a C# or .NET programmer, so I can't help you there), but the size of the monitor is usually not available. If an estimate is good enough then have the user enter the size of the monitor (i.e. 21" or whatever) and solve for DPI:
在不知道 DPI 的情况下,在物理上没有真正的方法。像素是离散的,英寸不是,如果你在显示器上谈论英寸,你需要知道(至少)分辨率(和像素纵横比)和可见显示器区域的大小,以便计算你的 DPI . 分辨率通常可以在某处获取(我不是 C# 或 .NET 程序员,所以我无法帮助您),但通常无法提供监视器的大小。如果估计足够好,那么让用户输入显示器的尺寸(即 21" 或其他尺寸)并求解 DPI:
(resX/DPI)^2 + (resY/DPI)^2 = screenDiagonal^2
giving (assuming you know the diagonal and the resolution)
给予(假设你知道对角线和分辨率)
DPI = sqrt(resX^2+resY^2)/screenDiagonal
This is just an estimate, as monitors are never exactly 21" (.. or whatever), and the pixel aspect ratio is hardly ever exactly 1:1.
这只是一个估计值,因为显示器永远不会恰好是 21"(.. 或其他),并且像素纵横比几乎永远不会是 1:1。
If you're talking inches on paper, then, quite naturally you need to know the DPI of your printer (or, more accurately, the current printer settings).
如果您在纸上谈论英寸,那么很自然地您需要知道打印机的 DPI(或者更准确地说,当前的打印机设置)。
回答by Noldorin
You can create the Graphics object simply by calling this.CreateGraphics()
(or more generally Control.CreateGraphics()
) and then use the DpiX and DpiY properties as you seem to know. Just remember to dispose the graphics object after creating it (ideally with a Using statement).
您可以简单地通过调用this.CreateGraphics()
(或更一般地Control.CreateGraphics()
)来创建 Graphics 对象,然后使用您似乎知道的 DpiX 和 DpiY 属性。只需记住在创建图形对象后对其进行处理(最好使用 Using 语句)。
If you're not using WinForms, then please let us know what sort of application it is.
如果您不使用 WinForms,请告诉我们它是什么类型的应用程序。