DpiHelpers.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. namespace PDF_Office.Helper
  9. {
  10. internal class DpiHelpers
  11. {
  12. /// <summary>
  13. /// 当前系统DPI
  14. /// </summary>
  15. public static int Dpi { get; private set; }
  16. /// <summary>
  17. /// 当前DPI相对系统标准DPI(96)缩小比例
  18. /// </summary>
  19. public static float ScaleDpi { get { return (96F / Dpi); } }
  20. /// <summary>
  21. /// 系统DPI相对PDF文档DPI放大倍数
  22. /// </summary>
  23. public static double ScaleFactor { get { return (Dpi / 72D); } }
  24. /// <summary>
  25. /// PDF文档DPI相对系统标准DPI(96)缩小倍数
  26. /// </summary>
  27. public static double InvertScaleFactor { get { return (72D / Dpi); } }
  28. static DpiHelpers()
  29. {
  30. try
  31. {
  32. var flags = BindingFlags.NonPublic | BindingFlags.Static;
  33. var dpiProperty = typeof(SystemParameters).GetProperty("Dpi", flags);
  34. Dpi = (int)dpiProperty.GetValue(null, null);
  35. }
  36. catch { }
  37. }
  38. /// <summary>
  39. /// 从WPF获取的数值转换成标准DPI时的大小
  40. /// 缩小
  41. /// </summary>
  42. /// <param name="oldValue">当前数值</param>
  43. /// <returns></returns>
  44. public static int GetDpiUnrelatedNum(int oldValue)
  45. {
  46. return (int)(oldValue * ScaleDpi);
  47. }
  48. /// <summary>
  49. /// 从WPF获取的数值转换成标准DPI时的大小
  50. /// 缩小
  51. /// </summary>
  52. /// <param name="oldValue">当前数值</param>
  53. /// <returns></returns>
  54. public static double GetDpiUnrelatedNum(double oldValue)
  55. {
  56. return (double)(oldValue * ScaleDpi);
  57. }
  58. /// <summary>
  59. /// 标准DPI时的大小转换成当前DPI对应大小
  60. /// 放大
  61. /// </summary>
  62. /// <param name="oldValue">当前数值</param>
  63. /// <returns></returns>
  64. public static double GetDpiRelatedNum(double oldValue)
  65. {
  66. return (oldValue / ScaleDpi);
  67. }
  68. /// <summary>
  69. /// 当前矩形换成标准DPI时的大小
  70. /// </summary>
  71. /// <param name="oldValue"></param>
  72. /// <returns></returns>
  73. public static Rect GetDpiUnrelatedRect(Rect oldValue)
  74. {
  75. return new Rect(GetDpiUnrelatedNum(oldValue.Left), GetDpiUnrelatedNum(oldValue.Top),
  76. GetDpiUnrelatedNum(oldValue.Width), GetDpiUnrelatedNum(oldValue.Height));
  77. }
  78. /// <summary>
  79. /// 转换到PDF矩形
  80. /// </summary>
  81. /// <param name="oldValue"></param>
  82. /// <returns></returns>
  83. public static Rect GetRawRect(Rect oldValue)
  84. {
  85. Rect standRect = new Rect(GetDpiUnrelatedNum(oldValue.Left), GetDpiUnrelatedNum(oldValue.Top),
  86. GetDpiUnrelatedNum(oldValue.Width), GetDpiUnrelatedNum(oldValue.Height));
  87. return new Rect((int)(standRect.Left * 72D / 96D), (int)(standRect.Top * 72D / 96D), (int)(standRect.Width * 72D / 96D), (int)(standRect.Height * 72D / 96D));
  88. }
  89. /// <summary>
  90. /// 当前矩形换成当前DPI时的矩形大小
  91. /// </summary>
  92. /// <param name="oldValue"></param>
  93. /// <returns></returns>
  94. public static Rect GetDpiRelatedRect(Rect oldValue)
  95. {
  96. return new Rect(GetDpiRelatedNum(oldValue.Left), GetDpiRelatedNum(oldValue.Top),
  97. GetDpiRelatedNum(oldValue.Width), GetDpiRelatedNum(oldValue.Height));
  98. }
  99. /// <summary>
  100. /// 将PDF矩形,转换为当前DPI时的矩形大小
  101. /// </summary>
  102. /// <param name="standRect"></param>
  103. /// <returns></returns>
  104. public static Rect GetRawRelatedRect(Rect standRect)
  105. {
  106. Rect newRect = new Rect(standRect.Left / 72D *96D, standRect.Top / 72D * 96D, standRect.Width / 72D * 96D, standRect.Height / 72D * 96D);
  107. return GetDpiRelatedRect(newRect);
  108. }
  109. /// <summary>
  110. /// 当前点转换成标准DPI时的大小
  111. /// </summary>
  112. /// <param name="oldValue"></param>
  113. /// <returns></returns>
  114. public static Point GetDpiUnrelatedPoint(Point oldValue)
  115. {
  116. return new Point(GetDpiUnrelatedNum(oldValue.X), GetDpiUnrelatedNum(oldValue.Y));
  117. }
  118. /// <summary>
  119. /// 点换成当前DPI时的大小
  120. /// </summary>
  121. /// <param name="oldValue"></param>
  122. /// <returns></returns>
  123. public static Point GetDpiRelatedPoint(Point oldValue)
  124. {
  125. return new Point(GetDpiRelatedNum(oldValue.X), GetDpiRelatedNum(oldValue.Y));
  126. }
  127. /// <summary>
  128. /// 当前Size转换成标准DPI时的大小
  129. /// </summary>
  130. /// <param name="oldValue"></param>
  131. /// <returns></returns>
  132. public static Size GetDpiUnrelatedSize(Size oldValue)
  133. {
  134. return new Size(GetDpiUnrelatedNum(oldValue.Width), GetDpiUnrelatedNum(oldValue.Height));
  135. }
  136. /// <summary>
  137. /// Size换成当前DPI时的大小
  138. /// </summary>
  139. /// <param name="oldValue"></param>
  140. /// <returns></returns>
  141. public static Size GetDpiRelatedSize(Size oldValue)
  142. {
  143. return new Size(GetDpiRelatedNum(oldValue.Width), GetDpiRelatedNum(oldValue.Height));
  144. }
  145. }
  146. }