【问题标题】:acquire monitor physical dimension [duplicate]获取监视器物理尺寸[重复]
【发布时间】:2011-06-16 09:46:06
【问题描述】:

可能重复:
How do I determine the true pixel size of my Monitor in .NET?

如何获得显示器尺寸我的意思是它的物理尺寸如何宽度和高度以及对角线例如17英寸或什么

我不需要分辨率,我试过了

using System.Management ; 

namespace testscreensize
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("\\root\\wmi", "SELECT * FROM WmiMonitorBasicDisplayParams");

            foreach (ManagementObject mo in searcher.Get())
            {
                double width = (byte)mo["MaxHorizontalImageSize"] / 2.54;
                double height = (byte)mo["MaxVerticalImageSize"] / 2.54;
                double diagonal = Math.Sqrt(width * width + height * height);
                Console.WriteLine("Width {0:F2}, Height {1:F2} and Diagonal {2:F2} inches", width, height, diagonal);
            }

            Console.ReadKey();

        }
    }
}

报错

找不到类型或命名空间名称'ManagementObjectSearcher'

它仅适用于 vista,我需要更广泛的解决方案

我也试过了

Screen.PrimaryScreen.Bounds.Height

但它会返回分辨率

【问题讨论】:

标签: c# winforms


【解决方案1】:

您可以将GetDeviceCaps() WinAPI 与HORZSIZEVERTSIZE 参数一起使用。

[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

private const int HORZSIZE = 4;
private const int VERTSIZE = 6;
private const double MM_TO_INCH_CONVERSION_FACTOR = 25.4;

void  Foo()
{
    var hDC = Graphics.FromHwnd(this.Handle).GetHdc();
    int horizontalSizeInMilliMeters = GetDeviceCaps(hDC, HORZSIZE);
    double horizontalSizeInInches = horizontalSizeInMilliMeters / MM_TO_INCH_CONVERSION_FACTOR;
    int vertivalSizeInMilliMeters = GetDeviceCaps(hDC, VERTSIZE);
    double verticalSizeInInches = vertivalSizeInMilliMeters / MM_TO_INCH_CONVERSION_FACTOR;
}

【讨论】:

  • 这应该是选择的答案。它对我来说很完美。我什至可以复制/粘贴。这里唯一没有明确说明的是这需要System.Runtime.InteropServices 命名空间。
【解决方案2】:

您可以通过SystemInformation.PrimaryMonitorSize.WidthSystemInformation.PrimaryMonitorSize.Height获取当前屏幕的屏幕分辨率。您可以从 Graphics 对象获得的每英寸像素数:Graphics.DpiXGraphics.DpiY。其余的只是一个简单的方程(毕达哥拉斯)。我希望这会有所帮助, 大卫。

【讨论】:

  • 双 MonitorSize = Math.Sqrt(Math.Pow(SystemInformation.PrimaryMonitorSize.Width / Graphics.DpiX) + Math.Pow(SystemInformation.PrimaryMonitorSize.Height / Graphics.DpiY))
  • 我认为应该可以,虽然我还没有测试过
  • 执行我你指的是哪个图形,请原谅我,但我是新手
  • 对不起,我没有说清楚。 Graphics 对象用于绘画,例如图像或控件。它包含在 System.Drawing 命名空间中。您还可以从桌面获取Graphics 对象。这个Graphics 对象应该包含整个屏幕的 DPI。您可以使用以下代码获取Graphics 对象:DesktopGraphics = Graphics.FromHdc(GetDC(IntPtr.Zero));。然后,您可以使用上面的代码计算显示器尺寸(以英寸为单位)(不过,您需要使用 DesktopGraphics 而不是 Graphics)。我希望这是您正在寻找的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-22
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
  • 2018-04-22
  • 1970-01-01
  • 2019-02-24
相关资源
最近更新 更多