我如何确定我在.NET监视器的真正像素大小?我在、监视器、像素、大小

2023-09-02 01:33:26 作者:白衬衫男孩

我想在我的应用程序显示在真实尺寸的图像。为此我需要知道显示器的像素尺寸。

I want to display an image at 'true size' in my application. For that I need to know the pixel size of the display.

我知道Windows显示分辨率名义上是96DPI,但我的目的,我希望有一个更好的猜测。我理解这些信息可能并不总是可用的或准确(例如,旧的CRT显示器),但我想用液晶显示器的prevelance,这应该是可能的!

I know windows display resolution is nominally 96dpi, but for my purposes I want a better guess. I understand this information may not always be available or accurate (e.g. older CRT displays), but I imagine with the prevelance of LCD displays that this should be possible!

有没有办法让我的显示器的像素尺寸是多少?

Is there a way to get the pixel size of my display?

有没有一种方法来确定像素大小是准确的?

Is there a way to determine if the pixel size is accurate?

.NET API的preferred(我无法找到它们),但Win32的是也没关系,我很高兴的P / Invoke。

.NET API's preferred (I couldn't find them), but Win32 is OK too, I'm happy to P/Invoke.

推荐答案

有关的显示尺寸,你会想Screen.PrimaryScreen.Bounds.Size (或 Screen.GetBounds(MyForm的))。

For the display size you'll want Screen.PrimaryScreen.Bounds.Size (or Screen.GetBounds(myform)).

如果您希望DPI,使用 DpiX 和< A HREF =htt​​p://msdn.microsoft.com/en-us/library/system.drawing.graphics.dpiy.aspx> DpiY 中的图形:

If you want the DPI, use the DpiX and DpiY properties of Graphics:

PointF dpi = PointF.Empty;
using(Graphics g = this.CreateGraphics()){
    dpi.X = g.DpiX;
    dpi.Y = g.DpiY;
}

哦,等等!你想实际,举行统治者到监测和衡量,大小?不,不是可以使用的任意的OS服务。 OS不知道在监视器的实际尺寸,或者用户如何它校准。有些信息是理论上探测,但它没有足够确定性的OS可靠地使用它,所以也没有。

Oh, wait! You wanted actual, hold a ruler up to the monitor and measure, size?! No. Not possible using any OS services. The OS doesn't know the actual dimensions of the monitor, or how the user has it calibrated. Some of this information is theoretically detectable, but it's not deterministic enough for the OS to use it reliably, so it doesn't.

作为变通,你可以尝试一些东西。

As a work around, you can try a couple of things.

您可以尝试查询已安装的监控装置的显示字符串(我不知道如何做到这一点),看看是否可以解析出一个合理的大小了这一点。例如,显示器可能是一个ValueBin E17p,而你的可能的推断,这是一个17从监测。当然,这显示字符串可能是即插即用监视器。该方案是pretty的粗略的最好的。 您可以的要求的用户是什么大小的显示器他们。的也许的他们会知道。 You can try to query the display string of the installed monitor device (I'm not sure how to do that) and see if you can parse out a sensible size out of that. For example, the monitor might be a "ValueBin E17p", and you might deduce that it's a 17" monitor from that. Of course, this display string is likely to be "Plug and Play Monitor". This scheme is pretty sketchy at best. You could ask the user what size monitor they have. Maybe they'll know.

一旦你知道(或认为的你知道)显示器的对角线尺寸,你需要找到它的物理长宽比。此外,两件事情:

Once you know (or think you know) the monitor's diagonal size, you need to find its physical aspect ratio. Again, a couple of things:

假设当前像素宽高比显示器的物理长宽比相匹配。这是假设(一)用户选择的分辨率,非常适合自己的显示器,以及(b)将显示器有正方形像素。我不知道当前的面向消费者的电脑显示器不具有方形像素,但旧的没有和较新的可能。 询问用户。的也许的他们会知道。 Assume the current pixel aspect ratio matches the monitor's physical aspect ratio. This assumes that (A) the user has chosen a resolution that is ideal for their monitor, and that (B) the monitor has square pixels. I don't know of a current consumer-oriented computer monitor that doesn't have square pixels, but older ones did and newer ones might. Ask the user. Maybe they'll know.

一旦你知道(或认为的你知道)显示器的对角线尺寸和物理纵横比,那么你就可以计算出它的物理宽度和高度。 A 2 + B 2 = C 2 ,所以一些计算将提供给你不错的:

Once you know (or think you know) what the monitor's diagonal size and physical aspect ratio are, then you you can calculate it's physical width and height. A2 + B2 = C2, so a few calculations will give it to you good:

如果你发现这是一个17显示器,其目前的分辨率为1280×1024:   1280 2 + 1024 2 = 2686976   SQRT(2686976)= 1639.1998047828092637409837247032   17* 1280 / 1639.2 = 13.274768179599804782820888238165   17* 1024 / 1639.2 = 10.619814543679843826256710590532

If you found out that it's a 17" monitor, and its current resolution is 1280 x 1024: 12802 + 10242 = 2686976 Sqrt(2686976) = 1639.1998047828092637409837247032 17" * 1280 / 1639.2 = 13.274768179599804782820888238165" 17" * 1024 / 1639.2 = 10.619814543679843826256710590532"

这使物理宽度13.27与物理高度10.62。这使得像素13.27/ 1280 = 10.62/ 1024 = 0.01037或有关0.263毫米。

This puts the physical width at 13.27" and the physical height at 10.62". This makes the pixels 13.27" / 1280 = 10.62" / 1024 = 0.01037" or about 0.263 mm.

当然,所有这一切都是无效的,如果用户不具有合适的分辨率,显示器具有古怪非方形像素,或它的一个较旧的模拟监视器和控制,不进行调整适当的显示,以填补整个物理屏幕。更糟的是,它可能是一台投影机。

Of course, all of this is invalid if the user doesn't have a suitable resolution, the monitor has wacky non-square pixels, or it's an older analog monitor and the controls aren't adjusted properly for the display to fill the entire physical screen. Or worse, it could be a projector.

在最后,你可能是最好关闭进行校准步骤,你必须在用户的实际持有一把尺子到屏幕的,并测量了东西给你的大小。你可以:

In the end, you may be best off performing a calibration step where you have the user actually hold a ruler up to the screen, and measure the size of something for you. You could:

让用户点击鼠标在任意两点一英寸(或厘米)分开。 在屏幕上绘制一个框,让用户preSS向上和向下箭头来调整它的高度,和左,右箭头来调整它的宽度,直到框,正是1英寸(或厘米)见方根据他们的统治者。 在屏幕上绘制一个框,让用户告诉你多少英寸/厘米它在每个维度。

无论你做什么,不要指望你的结果是100%准确。有太多的因素在起作用您(或用户),以获得这完全正确的,每一次。

No matter what you do, don't expect your results to be 100% accurate. There are way too many factors at play for you (or the user) to get this exactly correct, every time.

请注意,96 dpi的通常的 pretty的代码的是准确的。现代像素非预计的屏幕都倾向于约为0.25毫米,而奋斗,所以通常结束了的有关的每英寸100个物理像素,给予或采取,如果显示器被设置为原始分辨率。 (当然,这是一个巨大的概括和做的不可以适用于所有的显示器。的Eee PC机,例如,具有像素约0.19毫米大小,如果我没有记错的规格。)

Be aware that 96 dpi is usually pretty close to accurate. Modern pixels on non-projected screens all tend to be about 0.25 mm, give or take, so you usually end up with about 100 physical pixels per inch, give or take, if the monitor is set to its native resolution. (Of course, this is a huge generalization and does not apply to all monitors. Eee PCs, for example, have pixels about 0.19 mm in size, if I remember the specs correctly.)