Browse Source

ComPDFKit(win) - 属性面板替换图片传入解码byte数组

weixiangjie 9 months ago
parent
commit
691026659e

+ 27 - 1
Demo/Examples/Compdfkit_Tools/Edit/PDFImageEdit/PDFImageEditControl/PDFImageEditControl.xaml.cs

@@ -17,6 +17,7 @@ using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Controls.Primitives;
 using System.Windows.Input;
+using System.Windows.Media;
 using System.Windows.Media.Imaging;
 
 namespace Compdfkit_Tools.Edit
@@ -298,9 +299,34 @@ namespace Compdfkit_Tools.Edit
                     GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
                     if (imageArea != null)
                     {
+                        int imageWidth = 0;
+                        int imageHeight = 0;
+                        byte[] imageData = null;
+                        
+                        BitmapFrame frame = null;
+                        BitmapDecoder decoder = BitmapDecoder.Create(new Uri(openFileDialog.FileName), BitmapCreateOptions.None, BitmapCacheOption.Default);
+                        if (decoder.Frames.Count > 0)
+                        {
+                            frame = decoder.Frames[0];
+                        }
+                        if (frame != null)
+                        {
+                            imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
+                            if (frame.Format != PixelFormats.Bgra32)
+                            {
+                                FormatConvertedBitmap covert = new FormatConvertedBitmap(frame, PixelFormats.Bgra32, frame.Palette, 0);
+                                covert.CopyPixels(imageData, frame.PixelWidth * 4, 0);
+                            }
+                            else
+                            {
+                                frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
+                            }
+                            imageWidth = frame.PixelWidth;
+                            imageHeight = frame.PixelHeight;
+                        }
                         Rect oldRect = DataConversionForWPF.CRectConversionForRect(imageArea.GetFrame());
                         CRect imageRect = imageArea.GetClipRect();
-                        if (imageArea.ReplaceImageArea(imageRect, openFileDialog.FileName, string.Empty))
+                        if (imageArea.ReplaceImageArea(imageRect, imageData, imageWidth, imageHeight))
                         {
                             PDFEditHistory editHistory = new PDFEditHistory();
                             editHistory.EditPage = editPage;