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();
                }
            }
View Code

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

using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
            {
                float dpiX = graphics.DpiX;
                float dpiY = graphics.DpiY;
            }
View Code

 

posted @ 2013-07-12 20:21  Lee's Blog  阅读(14042)  评论(1编辑  收藏  举报