Browse Source

签名-签名字体裁剪问题

zhuyi 2 years ago
parent
commit
4c9110cb03

+ 32 - 32
PDF Office/ViewModels/PropertyPanel/AnnotPanel/SignatureCreateDialogViewModel.cs

@@ -11,6 +11,7 @@ using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Drawing;
 using System.Drawing.Imaging;
+using System.Globalization;
 using System.IO;
 using System.Linq;
 using System.Text;
@@ -442,37 +443,36 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
         /// <param name="fontcolor">字体颜色</param>
         /// <param name="backColor">背景颜色</param>
         /// <returns></returns>
-        private Bitmap TextToBitmap(string text, Font font, Rectangle rect, System.Drawing.Color fontcolor, System.Drawing.Color backColor)
+        private Bitmap TextToBitmap(string text, string FontFamily,double size, Rectangle rect, System.Windows.Media.Brush fontcolor, System.Drawing.Color backColor)
         {
-            Graphics g;
-            Bitmap bmp;
-            StringFormat format = new StringFormat(StringFormatFlags.NoClip);
-            if (rect == Rectangle.Empty)
-            {
-                bmp = new Bitmap(1, 1);
-                g = Graphics.FromImage(bmp);
-                //计算绘制文字所需的区域大小(根据宽度计算长度),重新创建矩形区域绘图
-                SizeF sizef = g.MeasureString(text, font, PointF.Empty, format);
+            FormattedText formatText = new FormattedText(text,
+            CultureInfo.CurrentCulture,
+            FlowDirection.LeftToRight,
+            new Typeface(new System.Windows.Media.FontFamily(FontFamily), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
+            size,
+            fontcolor, Helper.DpiHelpers.Dpi / 96F);
 
-                int width = (int)(sizef.Width + 1);
-                int height = (int)(sizef.Height + 1);
-                rect = new Rectangle(0, 0, width, height);
-                bmp.Dispose();
+            DrawingVisual drawingVisual = new DrawingVisual();
+            DrawingContext dc = drawingVisual.RenderOpen();
+            dc.DrawText(formatText,new System.Windows.Point(0,0));
+            dc.Close();
 
-                bmp = new Bitmap(width, height);
-            }
-            else
-            {
-                bmp = new Bitmap(rect.Width, rect.Height);
-            }
 
-            g = Graphics.FromImage(bmp);
+            Rect x = drawingVisual.ContentBounds;
+            Rect DrawRect = new Rect(0, 0, x.Width + 2, x.Height + 2);
+
+            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)DrawRect.Width+2, (int)DrawRect.Height+2, 96F, 96F, PixelFormats.Pbgra32);
+            renderTargetBitmap.Render(drawingVisual);
+
+            MemoryStream stream = new MemoryStream();
+            BitmapEncoder encoder = new PngBitmapEncoder();
+
+            encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
+            encoder.Save(stream);
+
+            Bitmap bitmap = new Bitmap(stream);
 
-            //使用ClearType字体功能
-            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
-            g.FillRectangle(new SolidBrush(backColor), rect);
-            g.DrawString(text, font, new SolidBrush(fontcolor), rect, format);
-            return bmp;
+            return bitmap;
         }
 
         private void Cancel()
@@ -491,26 +491,26 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
                             Cancel();
                             return;
                         }
-                        System.Drawing.Color fontcolor = System.Drawing.Color.Black;
+                        System.Windows.Media.Brush fontcolor = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#252629"));
                         switch (RadioButtonIndex)
                         {
                             case 1:
-                                fontcolor = System.Drawing.Color.Black;
+                                fontcolor = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#252629"));
                                 break;
                             case 2:
-                                fontcolor = System.Drawing.Color.Red;
+                                fontcolor = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#F3465B"));
                                 break;
                             case 3:
-                                fontcolor = System.Drawing.Color.Green;
+                                fontcolor = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#273C62"));
                                 break;
                             case 4:
-                                fontcolor = System.Drawing.Color.Blue;
+                                fontcolor = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#94989C"));
                                 break;
                             default:
                                 break;
                         }
 
-                        Bitmap bmp = TextToBitmap(InputText, new Font(FontNameList[fontNameIndex], 48), Rectangle.Empty, fontcolor, System.Drawing.Color.Transparent);
+                        Bitmap bmp = TextToBitmap(InputText, FontNameList[fontNameIndex], 20, Rectangle.Empty, fontcolor, System.Drawing.Color.Transparent);
                         string guid = Guid.NewGuid().ToString();
                         string path = System.IO.Path.Combine(App.CachePath.SignatureStampPath, guid);
                         bmp.Save(path, ImageFormat.Png);