Bläddra i källkod

签名-选中去背功能后,添加的新图片也应该应用去背

zhuyi 1 år sedan
förälder
incheckning
4f3588321c
1 ändrade filer med 22 tillägg och 41 borttagningar
  1. 22 41
      PDF Office/ViewModels/Dialog/SignatureCreateDialogViewModel.cs

+ 22 - 41
PDF Office/ViewModels/Dialog/SignatureCreateDialogViewModel.cs

@@ -28,7 +28,7 @@ namespace PDF_Master.ViewModels.PropertyPanel.AnnotPanel
         /// <summary>
         /// 非绘制时展示的画笔光标
         /// </summary>
-        private const double StrokeWidth= 3;
+        private const double StrokeWidth = 3;
         private const double StrokeHigh = 3;
         public string Title => "";
         private CPDFViewer PDFViewer;
@@ -259,7 +259,7 @@ namespace PDF_Master.ViewModels.PropertyPanel.AnnotPanel
             DrawingAttributeObject = new DrawingAttributes();
             StrokesObject = new StrokeCollection();
             FontNameList = new ObservableCollection<string>();
-            ThicknessList = new ObservableCollection<string>() {"1.0pt", "2.0pt", "4.0pt", "6.0pt", "8.0pt" };//改了这里记得去改ThicknessListIndex的Set方法里面的取值
+            ThicknessList = new ObservableCollection<string>() { "1.0pt", "2.0pt", "4.0pt", "6.0pt", "8.0pt" };//改了这里记得去改ThicknessListIndex的Set方法里面的取值
             CancelCommand = new DelegateCommand(Cancel);
             CreateCommnad = new DelegateCommand(Create);
             UpdataDrawingStrokesCommnad = new DelegateCommand(UpdataDrawingStrokes);
@@ -337,44 +337,25 @@ namespace PDF_Master.ViewModels.PropertyPanel.AnnotPanel
                 BitmapImage image = new BitmapImage(new Uri(openFile.FileName));
                 double scale = Math.Min((double)600 / image.PixelWidth, (double)600 / image.PixelHeight);
                 scale = Math.Min(scale, 1);
-                string ext = Path.GetExtension(openFile.FileName);
-                if (ext.ToUpper() == ".PNG")
+                BitmapEncoder encoder = new PngBitmapEncoder();
+                var targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
+                encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
+                path = System.IO.Path.Combine(path, name);
+                using (FileStream stream = new FileStream(path, FileMode.Create))
                 {
-                    BitmapEncoder encoder = new PngBitmapEncoder();
-                    var targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
-                    encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
-                    path = System.IO.Path.Combine(path, name);
-                    using (FileStream stream = new FileStream(path, FileMode.Create))
-                    {
-                        encoder.Save(stream);
-                    }
-                    if (!string.IsNullOrEmpty(SaveToPath))
-                    {
-                        App.CachePath.AddToDeleteFiles(SaveToPath);
-                    }
-                    IsNewFile = true;
-                    SaveToPath = path;
-                    ImagePreviewSource = originalimagePreviewSource = targetBitmap;
-                    ShowImageButton = Visibility.Collapsed;
+                    encoder.Save(stream);
                 }
-                else
+                if (!string.IsNullOrEmpty(SaveToPath))
                 {
-                    BitmapEncoder encoder = new JpegBitmapEncoder();
-                    TransformedBitmap targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
-                    encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
-                    path = System.IO.Path.Combine(path, name);
-                    using (FileStream stream = new FileStream(path, FileMode.Create))
-                    {
-                        encoder.Save(stream);
-                    }
-                    if (!string.IsNullOrEmpty(SaveToPath))
-                    {
-                        App.CachePath.AddToDeleteFiles(SaveToPath);
-                    }
-                    IsNewFile = true;
-                    SaveToPath = path;
-                    ImagePreviewSource = originalimagePreviewSource = targetBitmap;
-                    ShowImageButton = Visibility.Collapsed;
+                    App.CachePath.AddToDeleteFiles(SaveToPath);
+                }
+                IsNewFile = true;
+                SaveToPath = path;
+                ImagePreviewSource = originalimagePreviewSource = targetBitmap;
+                ShowImageButton = Visibility.Collapsed;
+                if (IsRemoveBackground)
+                {
+                    ImagePreviewSource = removeBackgroundImagePreviewSource = ToBitmapImage(KnockOutGzf());
                 }
             }
             else
@@ -452,7 +433,7 @@ namespace PDF_Master.ViewModels.PropertyPanel.AnnotPanel
         /// <param name="fontcolor">字体颜色</param>
         /// <param name="backColor">背景颜色</param>
         /// <returns></returns>
-        private Bitmap TextToBitmap(string text, string FontFamily,double size, Rectangle rect, System.Windows.Media.Brush 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)
         {
             FormattedText formatText = new FormattedText(text,
             CultureInfo.CurrentCulture,
@@ -463,14 +444,14 @@ namespace PDF_Master.ViewModels.PropertyPanel.AnnotPanel
 
             DrawingVisual drawingVisual = new DrawingVisual();
             DrawingContext dc = drawingVisual.RenderOpen();
-            dc.DrawText(formatText,new System.Windows.Point(2,10));
+            dc.DrawText(formatText, new System.Windows.Point(2, 10));
             dc.Close();
 
 
             Rect x = drawingVisual.ContentBounds;
-            Rect DrawRect = new Rect(0, 0, x.Width + 2, x.Height +10);
+            Rect DrawRect = new Rect(0, 0, x.Width + 2, x.Height + 10);
 
-            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)DrawRect.Width+2, (int)DrawRect.Height+10, 96F, 96F, PixelFormats.Pbgra32);
+            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)DrawRect.Width + 2, (int)DrawRect.Height + 10, 96F, 96F, PixelFormats.Pbgra32);
             renderTargetBitmap.Render(drawingVisual);
 
             MemoryStream stream = new MemoryStream();