c#,使用WMI对象获取系统的DPI。

在使用WMI对象前,先要添加对System.Management的引用,然后就可以调用WMI对象。

我们使用的WMI对象是:Win32_DesktopMonitor

对象参考:http://msdn.microsoft.com/zh-cn/library/Aa394122

代码:

        static void Main(string[] args) {

using (ManagementClass mc = new ManagementClass("Win32_DesktopMonitor")) {
using (ManagementObjectCollection moc = mc.GetInstances()) {

int PixelsPerXLogicalInch = 0; // dpi for x
int PixelsPerYLogicalInch = 0; // dpi for y

foreach (ManagementObject each in moc) {
PixelsPerXLogicalInch = int.Parse((each.Properties["PixelsPerXLogicalInch"].Value.ToString()));
PixelsPerYLogicalInch = int.Parse((each.Properties["PixelsPerYLogicalInch"].Value.ToString()));
}

Console.WriteLine("PixelsPerXLogicalInch:" + PixelsPerXLogicalInch.ToString());
Console.WriteLine("PixelsPerYLogicalInch:" + PixelsPerYLogicalInch.ToString());
Console.Read();
}

}
}

以上代码在WIN7的环境下测试通过。

原文地址:https://www.cnblogs.com/kongxianghai/p/2434435.html