DpiHelpers.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. namespace ComPDFKitDemo.Helper
  13. {
  14. internal class Helpers
  15. {
  16. /// <summary>
  17. /// 当前系统DPI
  18. /// </summary>
  19. public static int Dpi { get; private set; }
  20. /// <summary>
  21. /// 当前DPI相对系统标准DPI(96)缩小比例
  22. /// </summary>
  23. public static float ScaleDpi { get { return (96F/Dpi); } }
  24. /// <summary>
  25. /// 系统DPI相对PDF文档DPI放大倍数
  26. /// </summary>
  27. public static double ScaleFactor { get { return (Dpi / 72D); } }
  28. /// <summary>
  29. /// PDF文档DPI相对系统标准DPI(96)缩小倍数
  30. /// </summary>
  31. public static double InvertScaleFactor { get { return (72D / Dpi); } }
  32. static Helpers()
  33. {
  34. try
  35. {
  36. var flags = BindingFlags.NonPublic | BindingFlags.Static;
  37. var dpiProperty = typeof(SystemParameters).GetProperty("Dpi", flags);
  38. Dpi = (int)dpiProperty.GetValue(null, null);
  39. }
  40. catch (Exception ex)
  41. {
  42. }
  43. }
  44. /// <summary>
  45. /// 从WPF获取的数值转换成标准DPI时的大小
  46. /// 缩小
  47. /// </summary>
  48. /// <param name="oldValue">当前数值</param>
  49. /// <returns></returns>
  50. public static int GetDpiUnrelatedNum(int oldValue)
  51. {
  52. return (int)(oldValue * ScaleDpi);
  53. }
  54. /// <summary>
  55. /// 从WPF获取的数值转换成标准DPI时的大小
  56. /// 缩小
  57. /// </summary>
  58. /// <param name="oldValue">当前数值</param>
  59. /// <returns></returns>
  60. public static double GetDpiUnrelatedNum(double oldValue)
  61. {
  62. return (double)(oldValue * ScaleDpi);
  63. }
  64. /// <summary>
  65. /// 标准DPI时的大小转换成当前DPI对应大小
  66. /// 放大
  67. /// </summary>
  68. /// <param name="oldValue">当前数值</param>
  69. /// <returns></returns>
  70. public static double GetDpiRelatedNum(double oldValue)
  71. {
  72. return (oldValue / ScaleDpi);
  73. }
  74. /// <summary>
  75. /// 当前矩形换成标准DPI时的大小
  76. /// </summary>
  77. /// <param name="oldValue"></param>
  78. /// <returns></returns>
  79. public static Rect GetDpiUnrelatedRect(Rect oldValue)
  80. {
  81. return new Rect(GetDpiUnrelatedNum(oldValue.Left), GetDpiUnrelatedNum(oldValue.Top),
  82. GetDpiUnrelatedNum(oldValue.Width), GetDpiUnrelatedNum(oldValue.Height));
  83. }
  84. /// <summary>
  85. /// 当前DPI转换到PDF矩形
  86. /// </summary>
  87. /// <param name="oldValue"></param>
  88. /// <returns></returns>
  89. public static Rect GetRawRect(Rect oldValue,bool isStandDpi=false)
  90. {
  91. Rect standRect = oldValue;
  92. if(!isStandDpi)
  93. {
  94. standRect=new Rect(GetDpiUnrelatedNum(oldValue.Left), GetDpiUnrelatedNum(oldValue.Top),
  95. GetDpiUnrelatedNum(oldValue.Width), GetDpiUnrelatedNum(oldValue.Height));
  96. }
  97. return new Rect((int)(standRect.Left * 72D / 96D),(int)(standRect.Top * 72D / 96D), (int)(standRect.Width * 72D / 96D),(int)(standRect.Height * 72D / 96D));
  98. }
  99. /// <summary>
  100. /// 当前矩形换成当前DPI时的矩形大小
  101. /// </summary>
  102. /// <param name="oldValue"></param>
  103. /// <returns></returns>
  104. public static Rect GetDpiRelatedRect(Rect oldValue)
  105. {
  106. return new Rect(GetDpiRelatedNum(oldValue.Left), GetDpiRelatedNum(oldValue.Top),
  107. GetDpiRelatedNum(oldValue.Width), GetDpiRelatedNum(oldValue.Height));
  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. }