c# 获取屏幕DPI

方法一:用ManagementClass来获取。需要引入System.Management.dll;

 using (ManagementClass mc = new ManagementClass("Win32_DesktopMonitor"))
{
using (ManagementObjectCollection moc = mc.GetInstances())
{ int PixelsPerXLogicalInch = ; // dpi for x
int PixelsPerYLogicalInch = ; // 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();
}
}

方法二:用Graphics来获取。需要引入 System.Drawing.dll ;

using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
{
float dpiX = graphics.DpiX;
float dpiY = graphics.DpiY;
}
上一篇:Python学习教程(learning Python)--3.3.1 Python下的布尔表达式


下一篇:用 EasyBCD 在 Win7/8 中硬盘安装 Ubuntu