DpiHelpers.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 { }
  41. }
  42. /// <summary>
  43. /// 从WPF获取的数值转换成标准DPI时的大小
  44. /// 缩小
  45. /// </summary>
  46. /// <param name="oldValue">当前数值</param>
  47. /// <returns></returns>
  48. public static int GetDpiUnrelatedNum(int oldValue)
  49. {
  50. return (int)(oldValue * ScaleDpi);
  51. }
  52. /// <summary>
  53. /// 从WPF获取的数值转换成标准DPI时的大小
  54. /// 缩小
  55. /// </summary>
  56. /// <param name="oldValue">当前数值</param>
  57. /// <returns></returns>
  58. public static double GetDpiUnrelatedNum(double oldValue)
  59. {
  60. return (double)(oldValue * ScaleDpi);
  61. }
  62. /// <summary>
  63. /// 标准DPI时的大小转换成当前DPI对应大小
  64. /// 放大
  65. /// </summary>
  66. /// <param name="oldValue">当前数值</param>
  67. /// <returns></returns>
  68. public static double GetDpiRelatedNum(double oldValue)
  69. {
  70. return (oldValue / ScaleDpi);
  71. }
  72. /// <summary>
  73. /// 当前矩形换成标准DPI时的大小
  74. /// </summary>
  75. /// <param name="oldValue"></param>
  76. /// <returns></returns>
  77. public static Rect GetDpiUnrelatedRect(Rect oldValue)
  78. {
  79. return new Rect(GetDpiUnrelatedNum(oldValue.Left), GetDpiUnrelatedNum(oldValue.Top),
  80. GetDpiUnrelatedNum(oldValue.Width), GetDpiUnrelatedNum(oldValue.Height));
  81. }
  82. /// <summary>
  83. /// 转换到PDF矩形
  84. /// </summary>
  85. /// <param name="oldValue"></param>
  86. /// <returns></returns>
  87. public static Rect GetRawRect(Rect oldValue)
  88. {
  89. Rect standRect= new Rect(GetDpiUnrelatedNum(oldValue.Left), GetDpiUnrelatedNum(oldValue.Top),
  90. GetDpiUnrelatedNum(oldValue.Width), GetDpiUnrelatedNum(oldValue.Height));
  91. return new Rect(standRect.Left * 72D / 96D, standRect.Top * 72D / 96D, standRect.Width * 72D / 96D, standRect.Height * 72D / 96D);
  92. }
  93. /// <summary>
  94. /// 当前矩形换成当前DPI时的矩形大小
  95. /// </summary>
  96. /// <param name="oldValue"></param>
  97. /// <returns></returns>
  98. public static Rect GetDpiRelatedRect(Rect oldValue)
  99. {
  100. return new Rect(GetDpiRelatedNum(oldValue.Left), GetDpiRelatedNum(oldValue.Top),
  101. GetDpiRelatedNum(oldValue.Width), GetDpiRelatedNum(oldValue.Height));
  102. }
  103. /// <summary>
  104. /// 当前点转换成标准DPI时的大小
  105. /// </summary>
  106. /// <param name="oldValue"></param>
  107. /// <returns></returns>
  108. public static Point GetDpiUnrelatedPoint(Point oldValue)
  109. {
  110. return new Point(GetDpiUnrelatedNum(oldValue.X), GetDpiUnrelatedNum(oldValue.Y));
  111. }
  112. /// <summary>
  113. /// 点换成当前DPI时的大小
  114. /// </summary>
  115. /// <param name="oldValue"></param>
  116. /// <returns></returns>
  117. public static Point GetDpiRelatedPoint(Point oldValue)
  118. {
  119. return new Point(GetDpiRelatedNum(oldValue.X), GetDpiRelatedNum(oldValue.Y));
  120. }
  121. /// <summary>
  122. /// 当前Size转换成标准DPI时的大小
  123. /// </summary>
  124. /// <param name="oldValue"></param>
  125. /// <returns></returns>
  126. public static Size GetDpiUnrelatedSize(Size oldValue)
  127. {
  128. return new Size(GetDpiUnrelatedNum(oldValue.Width), GetDpiUnrelatedNum(oldValue.Height));
  129. }
  130. /// <summary>
  131. /// Size换成当前DPI时的大小
  132. /// </summary>
  133. /// <param name="oldValue"></param>
  134. /// <returns></returns>
  135. public static Size GetDpiRelatedSize(Size oldValue)
  136. {
  137. return new Size(GetDpiRelatedNum(oldValue.Width), GetDpiRelatedNum(oldValue.Height));
  138. }
  139. }
  140. }