123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace PDF_Master.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((int)(standRect.Left * 72D / 96D), (int)(standRect.Top * 72D / 96D), (int)(standRect.Width * 72D / 96D), (int)(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>
- /// 将PDF矩形,转换为当前DPI时的矩形大小
- /// </summary>
- /// <param name="standRect"></param>
- /// <returns></returns>
- public static Rect GetRawRelatedRect(Rect standRect)
- {
- Rect newRect = new Rect(standRect.Left / 72D *96D, standRect.Top / 72D * 96D, standRect.Width / 72D * 96D, standRect.Height / 72D * 96D);
- return GetDpiRelatedRect(newRect);
- }
- /// <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));
- }
- }
- }
|