简体   繁体   English

在C#中将像素转换为英寸,反之亦然

[英]Convert Pixels to Inches and vice versa in C#

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 (eg I don't have the Graphics object, so that's not an option). 我知道我需要DPI,但我不知道如何获取这些信息(例如我没有Graphics对象,所以这不是一个选项)。

Is there a way? 有办法吗?

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. 例如,输出分辨率为1024x768,但DPI​​会因屏幕与投影机的距离而有所不同。 WPF, for example, always assumes 96 DPI on a video device. 例如,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);

There's, physically, no real way without knowing the DPI. 在没有了解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. 像素是离散的,英寸不是,如果你在显示器上说英寸,你需要知道(至少)分辨率(和像素长宽比)和可见监视区域的大小,以便计算你的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. 分辨率通常可以在某处获取(我不是C#或.NET程序员,所以我无法帮助你),但显示器的大小通常不可用。 If an estimate is good enough then have the user enter the size of the monitor (ie 21" or whatever) and solve for DPI: 如果估计值足够好,那么让用户输入监视器的大小(即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(或者更准确地说,当前的打印机设置)。

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. 您可以通过调用this.CreateGraphics() (或更一般的Control.CreateGraphics() )来创建Graphics对象,然后使用您似乎知道的DpiX和DpiY属性。 Just remember to dispose the graphics object after creating it (ideally with a Using statement). 只需记住在创建图形对象后处理它(理想情况下使用Using语句)。

If you're not using WinForms, then please let us know what sort of application it is. 如果您没有使用WinForms,请告诉我们它是什么类型的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM