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