|
@@ -0,0 +1,140 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Reflection;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using System.Windows;
|
|
|
+
|
|
|
+namespace PDF_Office.Helper
|
|
|
+{
|
|
|
+ internal class DpiHelpers
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// 当前系统DPI
|
|
|
+ /// </summary>
|
|
|
+ public static int Dpi { get; private set; }
|
|
|
+ /// <summary>
|
|
|
+ /// 当前DPI相对系统标准DPI(96)缩小比例
|
|
|
+ /// </summary>
|
|
|
+ public static float ScaleDpi { get { return (96F / Dpi); } }
|
|
|
+ /// <summary>
|
|
|
+ /// 系统DPI相对PDF文档DPI放大倍数
|
|
|
+ /// </summary>
|
|
|
+ public static double ScaleFactor { get { return (Dpi / 72D); } }
|
|
|
+ /// <summary>
|
|
|
+ /// PDF文档DPI相对系统标准DPI(96)缩小倍数
|
|
|
+ /// </summary>
|
|
|
+ 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 { }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 从WPF获取的数值转换成标准DPI时的大小
|
|
|
+ /// 缩小
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="oldValue">当前数值</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static int GetDpiUnrelatedNum(int oldValue)
|
|
|
+ {
|
|
|
+ return (int)(oldValue * ScaleDpi);
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 从WPF获取的数值转换成标准DPI时的大小
|
|
|
+ /// 缩小
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="oldValue">当前数值</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static double GetDpiUnrelatedNum(double oldValue)
|
|
|
+ {
|
|
|
+ return (double)(oldValue * ScaleDpi);
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 标准DPI时的大小转换成当前DPI对应大小
|
|
|
+ /// 放大
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="oldValue">当前数值</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static double GetDpiRelatedNum(double oldValue)
|
|
|
+ {
|
|
|
+ return (oldValue / ScaleDpi);
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 当前矩形换成标准DPI时的大小
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="oldValue"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static Rect GetDpiUnrelatedRect(Rect oldValue)
|
|
|
+ {
|
|
|
+ return new Rect(GetDpiUnrelatedNum(oldValue.Left), GetDpiUnrelatedNum(oldValue.Top),
|
|
|
+ GetDpiUnrelatedNum(oldValue.Width), GetDpiUnrelatedNum(oldValue.Height));
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 转换到PDF矩形
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="oldValue"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ 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(standRect.Left * 72D / 96D, standRect.Top * 72D / 96D, standRect.Width * 72D / 96D, standRect.Height * 72D / 96D);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 当前矩形换成当前DPI时的矩形大小
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="oldValue"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static Rect GetDpiRelatedRect(Rect oldValue)
|
|
|
+ {
|
|
|
+ return new Rect(GetDpiRelatedNum(oldValue.Left), GetDpiRelatedNum(oldValue.Top),
|
|
|
+ GetDpiRelatedNum(oldValue.Width), GetDpiRelatedNum(oldValue.Height));
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 当前点转换成标准DPI时的大小
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="oldValue"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static Point GetDpiUnrelatedPoint(Point oldValue)
|
|
|
+ {
|
|
|
+ return new Point(GetDpiUnrelatedNum(oldValue.X), GetDpiUnrelatedNum(oldValue.Y));
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 点换成当前DPI时的大小
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="oldValue"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static Point GetDpiRelatedPoint(Point oldValue)
|
|
|
+ {
|
|
|
+ return new Point(GetDpiRelatedNum(oldValue.X), GetDpiRelatedNum(oldValue.Y));
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 当前Size转换成标准DPI时的大小
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="oldValue"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static Size GetDpiUnrelatedSize(Size oldValue)
|
|
|
+ {
|
|
|
+ return new Size(GetDpiUnrelatedNum(oldValue.Width), GetDpiUnrelatedNum(oldValue.Height));
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// Size换成当前DPI时的大小
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="oldValue"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static Size GetDpiRelatedSize(Size oldValue)
|
|
|
+ {
|
|
|
+ return new Size(GetDpiRelatedNum(oldValue.Width), GetDpiRelatedNum(oldValue.Height));
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|