获取监视器物理尺寸
可能重复:
如何确定.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(); } } }
它给出了错误
找不到类型或命名空间名称'ManagementObjectSearcher'
它只适用于vista,我需要更广泛的解决方案
我也试过了
Screen.PrimaryScreen.Bounds.Height
但它会返回分辨率
您可以将GetDeviceCaps()
WinAPI与HORZSIZE
和VERTSIZE
参数一起使用。
[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; }
您可以使用SystemInformation.PrimaryMonitorSize.Width
和SystemInformation.PrimaryMonitorSize.Height
获取当前屏幕的屏幕分辨率。 您可以从Graphics
对象获得的每英寸像素数: Graphics.DpiX
和Graphics.DpiY
。 其余的只是一个简单的等式(毕达哥拉斯)。 大卫,我希望有所帮助。