c# 获取屏幕DPI
方法一:用ManagementClass来获取。需要引入System.Management.dll;
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(); } }
方法二:用Graphics来获取。需要引入 System.Drawing.dll ;
using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero)) { float dpiX = graphics.DpiX; float dpiY = graphics.DpiY; }
作者:dingli
出处:http://www.cnblogs.com/dingli/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。