Bläddra i källkod

图章-自定义图章去背相关

zhuyi 2 år sedan
förälder
incheckning
ddc4794dfd

+ 95 - 3
PDF Office/ViewModels/PropertyPanel/AnnotPanel/CustomCreateDialogViewModel.cs

@@ -109,6 +109,37 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
             }
         }
 
+        /// <summary>
+        /// 用于判断是否更换了文件,更换了文件则需要重新计算去背功能
+        /// </summary>
+        private bool IsNewFile = false;
+
+        private BitmapSource originalimagePreviewSource;
+        private BitmapSource removeBackgroundImagePreviewSource;
+
+        private bool issRemoveBackground;
+
+        public bool IsRemoveBackground
+        {
+            get { return issRemoveBackground; }
+            set
+            {
+                SetProperty(ref issRemoveBackground, value);
+                if (issRemoveBackground)
+                {
+                    if (IsNewFile)
+                    {
+                      removeBackgroundImagePreviewSource = ToBitmapImage(KnockOutGzf());
+                    }
+                    ImagePreviewSource = removeBackgroundImagePreviewSource;
+                }
+                else
+                {
+                    ImagePreviewSource = originalimagePreviewSource;
+                }
+            }
+        }
+
 
         private C_TEXTSTAMP_SHAPE shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
 
@@ -127,11 +158,12 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
         }
 
         public string SaveToPath { get; private set; }
+        public string RemoveBackgroundSaveToPath { 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; }
-
+        private Bitmap bitmapProxy;
         public void SetStampStyle(int index)
         {
             RadioButtonIndex = index;
@@ -324,8 +356,9 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
                     {
                         App.CachePath.AddToDeleteFiles(SaveToPath);
                     }
+                    IsNewFile = true;
                     SaveToPath = path;
-                    ImagePreviewSource = targetBitmap;
+                    ImagePreviewSource= originalimagePreviewSource = targetBitmap;
                     StampWidth = targetBitmap.PixelWidth;
                     StampHeight = targetBitmap.PixelHeight;
                     Type = StampType.IMAGE_STAMP;
@@ -346,7 +379,8 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
                         App.CachePath.AddToDeleteFiles(SaveToPath);
                     }
                     SaveToPath = path;
-                    ImagePreviewSource = targetBitmap;
+                    IsNewFile = true;
+                    ImagePreviewSource = originalimagePreviewSource = targetBitmap;
                     StampWidth = targetBitmap.PixelWidth;
                     StampHeight = targetBitmap.PixelHeight;
                     Type = StampType.IMAGE_STAMP;
@@ -358,6 +392,64 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
                 SaveToPath = "";
             }
         }
+
+        /// <summary>
+        /// 去除白底
+        /// </summary>
+        public Bitmap KnockOutGzf()
+        {
+
+            System.Drawing.Image image = System.Drawing.Image.FromFile(SaveToPath);
+            bitmapProxy = new Bitmap(image);
+            for (int i = 0; i < bitmapProxy.Width; i++)
+            {
+                for (int j = 0; j < bitmapProxy.Height; j++)
+                {
+                    System.Drawing.Color c = bitmapProxy.GetPixel(i, j);
+                    if (!(c.R < 240 || c.G < 240 || c.B < 240))
+                    {
+                        bitmapProxy.SetPixel(i, j, System.Drawing.Color.Transparent);
+                    }
+                }
+            }
+            return bitmapProxy;
+        }
+
+        /// <summary>
+        /// 将 Bitmap 转化为 BitmapSource
+        /// </summary>
+        /// <param name="bmp"/>要转换的 Bitmap
+        /// <returns>转换后的 BitmapImage</returns>
+        private BitmapImage ToBitmapImage(System.Drawing.Bitmap bmp)
+        {
+            System.IO.MemoryStream ms = new System.IO.MemoryStream();
+            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
+
+            BitmapImage image = new BitmapImage();
+            image.BeginInit();
+            image.StreamSource = ms;
+            image.CacheOption = BitmapCacheOption.OnLoad;
+            image.EndInit();
+            //编码,缓存到本地
+            string path = App.CachePath.CustomStampPath;
+            string name = Guid.NewGuid().ToString();
+            BitmapEncoder encoder = new PngBitmapEncoder();
+            encoder.Frames.Add(BitmapFrame.Create(image));
+            path = System.IO.Path.Combine(path, name);
+            using (FileStream stream = new FileStream(path, FileMode.Create))
+            {
+                encoder.Save(stream);
+            }
+            if (!string.IsNullOrEmpty(RemoveBackgroundSaveToPath))
+            {
+                App.CachePath.AddToDeleteFiles(RemoveBackgroundSaveToPath);
+            }
+            RemoveBackgroundSaveToPath = path;
+            IsNewFile = false;
+            return image;
+        }
+
+
         public void OnDialogOpened(IDialogParameters parameters)
         {
             if (parameters != null)

+ 14 - 1
PDF Office/ViewModels/PropertyPanel/AnnotPanel/StampAnnotPropertyViewModel.cs

@@ -554,7 +554,19 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
             Stamp stamp = new Stamp();
             stamp.Author = "";
             stamp.Opacity = 1;
-            stamp.SourcePath = viewModel.SaveToPath;
+            if (viewModel.IsRemoveBackground)
+            {
+                if (!string.IsNullOrEmpty(viewModel.SaveToPath))
+                {
+                    App.CachePath.AddToDeleteFiles(viewModel.SaveToPath);
+                }
+                stamp.SourcePath = viewModel.RemoveBackgroundSaveToPath;
+            }
+            else
+            {
+                stamp.SourcePath = viewModel.SaveToPath;
+            }
+         
             stamp.StampText = viewModel.StampText;
             stamp.MaxWidth = (int)DpiHelpers.GetDpiUnrelatedNum(viewModel.StampWidth / 72D * DpiHelpers.Dpi);
             stamp.MaxHeight = (int)DpiHelpers.GetDpiUnrelatedNum(viewModel.StampHeight / 72D * DpiHelpers.Dpi);
@@ -566,6 +578,7 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
             stamp.IsCheckedTime = viewModel.IsCheckedTime;
             CustomStampList.Add(stamp);
 
+            //缓存数据
             PDFSettings.CustomStampList stamps = Settings.Default.CustomStampList;
             if (stamps == null)
             {

+ 7 - 2
PDF Office/Views/PropertyPanel/AnnotPanel/CustomCreateDialog.xaml

@@ -145,14 +145,18 @@
                         HorizontalContentAlignment="Center"
                         FontSize="16"
                         Width="86">
-                    <Grid>
+                    <Grid >
+                        <Grid.RowDefinitions>
+                            <RowDefinition Height="*"/>
+                            <RowDefinition Height="20"/>
+                        </Grid.RowDefinitions>
                         <customcontrol:PathButton
                         x:Name="BtnAdd"
                         Height="24" Width="24"  IconHeight="20"  IconWidth="20" 
                         Icon="{StaticResource Ic_AddButtonPath}"  IconFill="Red"
                         IconPress="{StaticResource Ic_AddButtonPath}" IconPressFill="#C04CF8"
                         IconMouseOver="{StaticResource Ic_AddButtonPath}" IconMouseOverFill="#C04CF8" Visibility="{Binding ShowImageButton}"  Command="{Binding OpenImageCommnad}"/>
-                        <Grid Visibility="{Binding ElementName=BtnAdd,Path=Visibility,Converter={StaticResource UnVisivleConvert}}">
+                        <Grid Background="Red" Visibility="{Binding ElementName=BtnAdd,Path=Visibility,Converter={StaticResource UnVisivleConvert}}">
                             <Grid.RowDefinitions>
                                 <RowDefinition Height="24"/>
                                 <RowDefinition Height="*"/>
@@ -167,6 +171,7 @@
                             IconPress="{StaticResource Ic_AddButtonPath}" IconPressFill="#C04CF8"
                             IconMouseOver="{StaticResource Ic_AddButtonPath}" IconMouseOverFill="#C04CF8" Command="{Binding OpenImageCommnad}"/>
                         </Grid>
+                        <CheckBox Grid.Row="1" Width="20" Height="20" IsChecked="{Binding IsRemoveBackground}"/>
                     </Grid>
                 </TabItem>
             </TabControl>