获取DPI

 #region 获取DPI

        public static float getLogPiex()
        {
            float returnValue = 96;
            try
            {
                RegistryKey key = Registry.CurrentUser;
                RegistryKey pixeKey = key.OpenSubKey("Control Panel\\Desktop");
                if (pixeKey != null)
                {
                    var pixels = pixeKey.GetValue("LogPixels");
                    if (pixels != null)
                    {
                        returnValue = float.Parse(pixels.ToString());
                    }
                    pixeKey.Close();
                }
                else
                {
                    pixeKey = key.OpenSubKey("Control Panel\\Desktop\\WindowMetrics");
                    if (pixeKey != null)
                    {
                        var pixels = pixeKey.GetValue("AppliedDPI");
                        if (pixels != null)
                        {
                            returnValue = float.Parse(pixels.ToString());
                        }
                        pixeKey.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                returnValue = 96;
            }
            return returnValue;
        }

        #endregion

//计算DPI

float dpi = getLogPiex();
float dpiRate = dpi / 96;

猜你喜欢

转载自blog.csdn.net/mehnr/article/details/80746563