DpiHelpers.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 PDFSettings
  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. /// 转换到PDF矩形
  86. /// </summary>
  87. /// <param name="oldValue"></param>
  88. /// <returns></returns>
  89. public static Rect GetRawRect(Rect oldValue)
  90. {
  91. Rect standRect= new Rect(GetDpiUnrelatedNum(oldValue.Left), GetDpiUnrelatedNum(oldValue.Top),
  92. GetDpiUnrelatedNum(oldValue.Width), GetDpiUnrelatedNum(oldValue.Height));
  93. return new Rect(standRect.Left * 72D / 96D, standRect.Top * 72D / 96D, standRect.Width * 72D / 96D, standRect.Height * 72D / 96D);
  94. }
  95. /// <summary>
  96. /// 当前矩形换成当前DPI时的矩形大小
  97. /// </summary>
  98. /// <param name="oldValue"></param>
  99. /// <returns></returns>
  100. public static Rect GetDpiRelatedRect(Rect oldValue)
  101. {
  102. return new Rect(GetDpiRelatedNum(oldValue.Left), GetDpiRelatedNum(oldValue.Top),
  103. GetDpiRelatedNum(oldValue.Width), GetDpiRelatedNum(oldValue.Height));
  104. }
  105. /// <summary>
  106. /// 当前点转换成标准DPI时的大小
  107. /// </summary>
  108. /// <param name="oldValue"></param>
  109. /// <returns></returns>
  110. public static Point GetDpiUnrelatedPoint(Point oldValue)
  111. {
  112. return new Point(GetDpiUnrelatedNum(oldValue.X), GetDpiUnrelatedNum(oldValue.Y));
  113. }
  114. /// <summary>
  115. /// 点换成当前DPI时的大小
  116. /// </summary>
  117. /// <param name="oldValue"></param>
  118. /// <returns></returns>
  119. public static Point GetDpiRelatedPoint(Point oldValue)
  120. {
  121. return new Point(GetDpiRelatedNum(oldValue.X), GetDpiRelatedNum(oldValue.Y));
  122. }
  123. /// <summary>
  124. /// 当前Size转换成标准DPI时的大小
  125. /// </summary>
  126. /// <param name="oldValue"></param>
  127. /// <returns></returns>
  128. public static Size GetDpiUnrelatedSize(Size oldValue)
  129. {
  130. return new Size(GetDpiUnrelatedNum(oldValue.Width), GetDpiUnrelatedNum(oldValue.Height));
  131. }
  132. /// <summary>
  133. /// Size换成当前DPI时的大小
  134. /// </summary>
  135. /// <param name="oldValue"></param>
  136. /// <returns></returns>
  137. public static Size GetDpiRelatedSize(Size oldValue)
  138. {
  139. return new Size(GetDpiRelatedNum(oldValue.Width), GetDpiRelatedNum(oldValue.Height));
  140. }
  141. }
  142. }