using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; namespace PDF_Master.Helper { internal class DpiHelpers { /// /// 当前系统DPI /// public static int Dpi { get; private set; } /// /// 当前DPI相对系统标准DPI(96)缩小比例 /// public static float ScaleDpi { get { return (96F / Dpi); } } /// /// 系统DPI相对PDF文档DPI放大倍数 /// public static double ScaleFactor { get { return (Dpi / 72D); } } /// /// PDF文档DPI相对系统标准DPI(96)缩小倍数 /// public static double InvertScaleFactor { get { return (72D / Dpi); } } static DpiHelpers() { try { var flags = BindingFlags.NonPublic | BindingFlags.Static; var dpiProperty = typeof(SystemParameters).GetProperty("Dpi", flags); Dpi = (int)dpiProperty.GetValue(null, null); } catch { } } /// /// 从WPF获取的数值转换成标准DPI时的大小 /// 缩小 /// /// 当前数值 /// public static int GetDpiUnrelatedNum(int oldValue) { return (int)(oldValue * ScaleDpi); } /// /// 从WPF获取的数值转换成标准DPI时的大小 /// 缩小 /// /// 当前数值 /// public static double GetDpiUnrelatedNum(double oldValue) { return (double)(oldValue * ScaleDpi); } /// /// 标准DPI时的大小转换成当前DPI对应大小 /// 放大 /// /// 当前数值 /// public static double GetDpiRelatedNum(double oldValue) { return (oldValue / ScaleDpi); } /// /// 当前矩形换成标准DPI时的大小 /// /// /// public static Rect GetDpiUnrelatedRect(Rect oldValue) { return new Rect(GetDpiUnrelatedNum(oldValue.Left), GetDpiUnrelatedNum(oldValue.Top), GetDpiUnrelatedNum(oldValue.Width), GetDpiUnrelatedNum(oldValue.Height)); } /// /// 转换到PDF矩形 /// /// /// public static Rect GetRawRect(Rect oldValue) { Rect standRect = new Rect(GetDpiUnrelatedNum(oldValue.Left), GetDpiUnrelatedNum(oldValue.Top), GetDpiUnrelatedNum(oldValue.Width), GetDpiUnrelatedNum(oldValue.Height)); return new Rect((int)(standRect.Left * 72D / 96D), (int)(standRect.Top * 72D / 96D), (int)(standRect.Width * 72D / 96D), (int)(standRect.Height * 72D / 96D)); } /// /// 当前矩形换成当前DPI时的矩形大小 /// /// /// public static Rect GetDpiRelatedRect(Rect oldValue) { return new Rect(GetDpiRelatedNum(oldValue.Left), GetDpiRelatedNum(oldValue.Top), GetDpiRelatedNum(oldValue.Width), GetDpiRelatedNum(oldValue.Height)); } /// /// 将PDF矩形,转换为当前DPI时的矩形大小 /// /// /// public static Rect GetRawRelatedRect(Rect standRect) { Rect newRect = new Rect(standRect.Left / 72D *96D, standRect.Top / 72D * 96D, standRect.Width / 72D * 96D, standRect.Height / 72D * 96D); return GetDpiRelatedRect(newRect); } /// /// 当前点转换成标准DPI时的大小 /// /// /// public static Point GetDpiUnrelatedPoint(Point oldValue) { return new Point(GetDpiUnrelatedNum(oldValue.X), GetDpiUnrelatedNum(oldValue.Y)); } /// /// 点换成当前DPI时的大小 /// /// /// public static Point GetDpiRelatedPoint(Point oldValue) { return new Point(GetDpiRelatedNum(oldValue.X), GetDpiRelatedNum(oldValue.Y)); } /// /// 当前Size转换成标准DPI时的大小 /// /// /// public static Size GetDpiUnrelatedSize(Size oldValue) { return new Size(GetDpiUnrelatedNum(oldValue.Width), GetDpiUnrelatedNum(oldValue.Height)); } /// /// Size换成当前DPI时的大小 /// /// /// public static Size GetDpiRelatedSize(Size oldValue) { return new Size(GetDpiRelatedNum(oldValue.Width), GetDpiRelatedNum(oldValue.Height)); } } }