获取监视器物理尺寸

AMH*_*AMH 6 c# winforms

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

如何获得显示器尺寸我的意思是它的物理尺寸如何宽度和高度和对角线例如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();

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它给出了错误

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

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

我也试过了

Screen.PrimaryScreen.Bounds.Height
Run Code Online (Sandbox Code Playgroud)

但它会返回分辨率

Bal*_*a R 5

您可以使用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;
}
Run Code Online (Sandbox Code Playgroud)