Browse Source

图章-图章大小问题修改,DPI换算方法

zhuyi 2 years ago
parent
commit
d31cfd9a31

+ 140 - 0
PDF Office/Helper/DpiHelpers.cs

@@ -0,0 +1,140 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+
+namespace PDF_Office.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(standRect.Left * 72D / 96D, standRect.Top * 72D / 96D, standRect.Width * 72D / 96D, 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>
+        /// 当前点转换成标准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));
+        }
+    }
+}

+ 1 - 0
PDF Office/PDF Office.csproj

@@ -241,6 +241,7 @@
     <Compile Include="EventAggregators\PageEditRefreshEvent.cs" />
     <Compile Include="Helper\CacheFilePath.cs" />
     <Compile Include="Helper\ConverterHelper.cs" />
+    <Compile Include="Helper\DpiHelpers.cs" />
     <Compile Include="Helper\EditToolsHelper.cs" />
     <Compile Include="Helper\AdvancedInvokeCommandAction.cs" />
     <Compile Include="Helper\ArrowHelper.cs" />

+ 4 - 0
PDF Office/ViewModels/PropertyPanel/AnnotPanel/CustomCreateDialogViewModel.cs

@@ -91,6 +91,8 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
 
         public string SaveToPath { get; private set; }
         public string StampTextDate { get; private set; }
+        public int StampWidth { get; private set; }
+        public int StampHeight { get; private set; }
         public StampType Type { get; private set; }
 
         public void SetStampStyle(int index)
@@ -235,6 +237,8 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
                 BitmapSource bps = BitmapSource.Create(width, height, 96, 96, fmt, null, bytes, (width * fmt.BitsPerPixel + 7) / 8);
                 ImageSource = bps;
                 StampTextDate = date;
+                StampWidth = stampWidth;
+                StampHeight = stampHeight;
                 Type = StampType.TEXT_STAMP;
             }
             else

+ 12 - 5
PDF Office/ViewModels/PropertyPanel/AnnotPanel/StampAnnotPropertyViewModel.cs

@@ -4,6 +4,7 @@ using ComPDFKit.PDFPage;
 using ComPDFKitViewer.AnnotEvent;
 using ComPDFKitViewer.PdfViewer;
 using Microsoft.Win32;
+using PDF_Office.Helper;
 using PDF_Office.Model;
 using PDF_Office.Model.AnnotPanel;
 using PDF_Office.Properties;
@@ -16,6 +17,7 @@ using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.IO;
 using System.Linq;
+using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows;
@@ -233,6 +235,7 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
             Args.MaxHeight = stamp.MaxHeight;
             Args.StampTextDate = stamp.StampTextDate;
             Args.TextColor = stamp.TextColor;
+            Args.TextSharp = stamp.TextSharp;
             if (!string.IsNullOrEmpty(stamp.SourcePath))
             {
                 BitmapImage image = new BitmapImage(new Uri(stamp.SourcePath));
@@ -298,13 +301,15 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
                 PixelFormat fmt = PixelFormats.Bgra32;
                 BitmapSource bps = BitmapSource.Create(width, height, 96, 96, fmt, null, bytes, (width * fmt.BitsPerPixel + 7) / 8);
 
-
+                var flags = BindingFlags.NonPublic | BindingFlags.Static;
+                var dpiProperty = typeof(SystemParameters).GetProperty("Dpi", flags);
+                int Dpi = (int)dpiProperty.GetValue(null, null);
 
                 standardStamp.Opacity = 1;
                 standardStamp.SourcePath = "";
                 standardStamp.StampText = DynamicStampText[i];
-                standardStamp.MaxWidth = stampWidth;
-                standardStamp.MaxHeight = stampHeight;
+                standardStamp.MaxWidth = (int)DpiHelpers.GetDpiUnrelatedNum(stampWidth / 72D * DpiHelpers.Dpi);
+                standardStamp.MaxHeight = (int)DpiHelpers.GetDpiUnrelatedNum(stampHeight / 72D * DpiHelpers.Dpi);
                 standardStamp.Type = StampType.TEXT_STAMP;
                 standardStamp.ImageSource = bps;
                 switch (DynamicColor[i])
@@ -436,10 +441,12 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
             stamp.Opacity = 1;
             stamp.SourcePath = viewModel.SaveToPath;
             stamp.StampText = viewModel.StampText;
-            stamp.MaxWidth = viewModel.ImageSource.PixelWidth;
-            stamp.MaxHeight = viewModel.ImageSource.PixelHeight;
+            stamp.MaxWidth = (int)DpiHelpers.GetDpiUnrelatedNum(viewModel.StampWidth/ 72D * DpiHelpers.Dpi);
+            stamp.MaxHeight = (int)DpiHelpers.GetDpiUnrelatedNum(viewModel.StampHeight / 72D * DpiHelpers.Dpi);
             stamp.StampTextDate = viewModel.StampTextDate;
             stamp.Type = viewModel.Type;
+            stamp.TextColor = (TextStampColor)(int)viewModel.Color;
+            stamp.TextSharp = (TextStampSharp)(int)viewModel.Shape;
             CustomStampList.Add(stamp);
         }
     }