Jelajahi Sumber

内容编辑-图片属性面板操作

zhuyi 11 bulan lalu
induk
melakukan
6636a51490

+ 68 - 37
Demo/Examples/Compdfkit_Tools/Edit/PDFImageEdit/PDFImageEditControl/PDFImageEditControl.xaml.cs

@@ -4,6 +4,7 @@ using ComPDFKit.PDFPage;
 using ComPDFKit.PDFPage.Edit;
 using ComPDFKit.Tool;
 using ComPDFKit.Tool.UndoManger;
+using ComPDFKit.Viewer.Helper;
 using ComPDFKitViewer;
 using Microsoft.Win32;
 using System;
@@ -15,6 +16,7 @@ using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Controls.Primitives;
 using System.Windows.Media;
+using System.Windows.Media.Imaging;
 
 namespace Compdfkit_Tools.Edit
 {
@@ -43,7 +45,14 @@ namespace Compdfkit_Tools.Edit
 
         public void SetPDFImageEditData(ImageEditParam newEvent)
         {
-            EditEvent = null;
+            if (newEvent.EditIndex < 0)
+            {
+                EditEvent = null;
+            }
+            else
+            {
+                EditEvent = newEvent;
+            }
             if (newEvent != null && newEvent.EditType == CPDFEditType.EditImage)
             {
                 SetImageTransparency(newEvent.Transparency);
@@ -63,6 +72,7 @@ namespace Compdfkit_Tools.Edit
             GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
             if (imageArea != null)
             {
+                Rect oldRect = DataConversionForWPF.CRectConversionForRect(imageArea.GetFrame());
                 bool result = false;
                 if (e)
                 {
@@ -82,6 +92,9 @@ namespace Compdfkit_Tools.Edit
                     }
 
                     ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
+                    //ToolView.UpDateRender(oldRect, imageArea);
+                    ToolView.GetCPDFViewer().UpDateRenderFrame();
+                    editPage.EndEdit();
                 }
 
                 SetImageThumb();
@@ -93,6 +106,7 @@ namespace Compdfkit_Tools.Edit
             GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
             if (imageArea != null)
             {
+                Rect oldRect = DataConversionForWPF.CRectConversionForRect(imageArea.GetFrame());
                 if (imageArea.Rotate((float)e))
                 {
                     PDFEditHistory editHistory = new PDFEditHistory();
@@ -104,6 +118,10 @@ namespace Compdfkit_Tools.Edit
 
                     ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
                     SetImageThumb();
+                    //ToolView.UpDateRender(oldRect, imageArea);
+
+                    ToolView.GetCPDFViewer().UpDateRenderFrame();
+                    editPage.EndEdit();
                 }
             }
         }
@@ -127,6 +145,7 @@ namespace Compdfkit_Tools.Edit
             GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
             if (imageArea != null)
             {
+                Rect oldRect = DataConversionForWPF.CRectConversionForRect(imageArea.GetFrame());
                 if (imageArea.SetImageTransparency((byte)(ImasgeOpacitySlider.Value * 255)))
                 {
                     PDFEditHistory editHistory = new PDFEditHistory();
@@ -138,6 +157,10 @@ namespace Compdfkit_Tools.Edit
 
                     ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
                     SetImageThumb();
+                    //ToolView.UpDateRender(oldRect, imageArea);
+
+                    ToolView.GetCPDFViewer().UpDateRenderFrame();
+                    editPage.EndEdit();
                 }
             }
         }
@@ -145,11 +168,11 @@ namespace Compdfkit_Tools.Edit
         private void SliderOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
         {
             Slider slider = sender as Slider;
-            if(OpacityTextBox != null)
+            if (OpacityTextBox != null)
             {
                 OpacityTextBox.Text = string.Format("{0}%", (int)(ImasgeOpacitySlider.Value * 100));
             }
-          
+
             if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
             {
                 return;
@@ -157,6 +180,7 @@ namespace Compdfkit_Tools.Edit
             GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
             if (imageArea != null)
             {
+                Rect oldRect = DataConversionForWPF.CRectConversionForRect(imageArea.GetFrame());
                 if (imageArea.SetImageTransparency((byte)(ImasgeOpacitySlider.Value * 255)))
                 {
                     PDFEditHistory editHistory = new PDFEditHistory();
@@ -168,6 +192,10 @@ namespace Compdfkit_Tools.Edit
 
                     ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
                     SetImageThumb();
+                    //ToolView.UpDateRender(oldRect, imageArea);
+
+                    ToolView.GetCPDFViewer().UpDateRenderFrame();
+                    editPage.EndEdit();
                 }
             }
         }
@@ -183,8 +211,9 @@ namespace Compdfkit_Tools.Edit
                     GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
                     if (imageArea != null)
                     {
-                        CRect imageRect= imageArea.GetClipRect();
-                        if (imageArea.ReplaceImageArea(imageRect,openFileDialog.FileName,string.Empty))
+                        Rect oldRect = DataConversionForWPF.CRectConversionForRect(imageArea.GetFrame());
+                        CRect imageRect = imageArea.GetClipRect();
+                        if (imageArea.ReplaceImageArea(imageRect, openFileDialog.FileName, string.Empty))
                         {
                             PDFEditHistory editHistory = new PDFEditHistory();
                             editHistory.EditPage = editPage;
@@ -195,6 +224,10 @@ namespace Compdfkit_Tools.Edit
 
                             ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
                             SetImageThumb();
+                            //ToolView.UpDateRender(oldRect, imageArea);
+
+                            ToolView.GetCPDFViewer().UpDateRenderFrame();
+                            editPage.EndEdit();
                         }
                     }
                 }
@@ -207,7 +240,7 @@ namespace Compdfkit_Tools.Edit
             {
                 //需要选中图片对象
                 // Dictionary<int, List<Bitmap>> imageDict = PDFView.GetSelectedImages();
-                Dictionary<int, List<Bitmap>> imageDict=new Dictionary<int, List<Bitmap>>();
+                Dictionary<int, List<Bitmap>> imageDict = new Dictionary<int, List<Bitmap>>();
                 if (imageDict != null && imageDict.Count > 0)
                 {
                     System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
@@ -240,7 +273,7 @@ namespace Compdfkit_Tools.Edit
 
         private void ImageClipBtn_Click(object sender, RoutedEventArgs e)
         {
-            
+
         }
 
         private void OpacityComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
@@ -258,34 +291,32 @@ namespace Compdfkit_Tools.Edit
 
         public void SetImageThumb()
         {
-            //if (PDFView != null && EditEvent!=null)
-            //{
-            //    try
-            //    {
-            //        Dictionary<int, List<Bitmap>> imageDict = PDFView.GetSelectedImages();
-            //        foreach (int pageIndex in imageDict.Keys)
-            //        {
-            //            List<Bitmap> imageList = imageDict[pageIndex];
-            //            if (imageList.Count > 0)
-            //            {
-            //                Bitmap bitmapImage = imageList[0];
-            //                MemoryStream memoryStream = new MemoryStream();
-
-            //                bitmapImage.Save(memoryStream, bitmapImage.RawFormat);
-            //                BitmapImage imageShow = new BitmapImage();
-            //                imageShow.BeginInit();
-            //                imageShow.StreamSource = memoryStream;
-            //                imageShow.EndInit();
-            //                ImageThumbUI.Source = imageShow;
-            //                break;
-            //            }
-            //        } 
-            //    }
-            //    catch (Exception ex)
-            //    {
-
-            //    }
-            //}
+            if (EditEvent != null)
+            {
+                try
+                {
+                    GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
+
+                    string path = Path.GetTempPath();
+                    string uuid = Guid.NewGuid().ToString("N");
+                    string imagePath = Path.Combine(path, uuid + ".tmp");
+                    imageArea.ExtractImage(imagePath);
+
+                    Bitmap bitmapImage = new Bitmap(imagePath);
+                    MemoryStream memoryStream = new MemoryStream();
+
+                    bitmapImage.Save(memoryStream, bitmapImage.RawFormat);
+                    BitmapImage imageShow = new BitmapImage();
+                    imageShow.BeginInit();
+                    imageShow.StreamSource = memoryStream;
+                    imageShow.EndInit();
+                    ImageThumbUI.Source = imageShow;
+                }
+                catch (Exception ex)
+                {
+
+                }
+            }
         }
 
         public void SetImageTransparency(double transparency)
@@ -294,11 +325,11 @@ namespace Compdfkit_Tools.Edit
             OpacityTextBox.Text = string.Format("{0}%", (int)(Math.Round(ImasgeOpacitySlider.Value * 100)));
         }
 
-        private void GetImageArea(out CPDFEditImageArea imageArea,out CPDFPage pdfPage, out CPDFEditPage editPage)
+        private void GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage)
         {
             imageArea = null;
             editPage = null;
-            pdfPage=null;
+            pdfPage = null;
             if (ToolView == null || EditEvent == null)
             {
                 return;