获取显示器的物理尺寸 [英] acquire monitor physical dimension

查看:78
本文介绍了获取显示器的物理尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何确定真实值.NET中显示器的像素大小?

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

How to get the monitor size I mean its physical size how width and height and diagonal for example 17 inch or what

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

I don't need the resolution , I tried

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'

The type or namespace name 'ManagementObjectSearcher' could not be found

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

and it works for vista only , I need much wider solution

我也尝试过

Screen.PrimaryScreen.Bounds.Height

但它返回分辨率

推荐答案

您可以使用

You can use the GetDeviceCaps() WinAPI with HORZSIZE and VERTSIZE parameters.

[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;
}

这篇关于获取显示器的物理尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆