获取监视器物理尺寸

时间:2011-06-15 20:13:39

标签: c# winforms

  

可能重复:
  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

但它会返回分辨率

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:4)

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