c# 獲取屏幕DPI

方法一:用ManagementClass來獲取。須要引入System.Management.dll;ide

 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 ;spa

using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
            {
                float dpiX = graphics.DpiX;
                float dpiY = graphics.DpiY;
            }
View Code
相關文章
相關標籤/搜索