Sfoglia il codice sorgente

内容编辑-图片替换传入解码byte数组

weixiangjie 9 mesi fa
parent
commit
6c58219fdf

+ 29 - 4
Demo/Examples/Compdfkit_Tools/Edit/ContentEditCOntrol/ContentEditControl.xaml.cs

@@ -12,6 +12,7 @@ using System.Collections.Generic;
 using System.ComponentModel;
 using System.Diagnostics;
 using System.Drawing;
+using System.Drawing.Imaging;
 using System.IO;
 using System.Runtime.CompilerServices;
 using System.Windows;
@@ -710,10 +711,34 @@ namespace Compdfkit_Tools.PDFControl
                     openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
                     if (openFileDialog.ShowDialog() == true)
                     {
-                        //byte[] fileData = File.ReadAllBytes(openFileDialog.FileName);
-                        //Bitmap bitmap = new Bitmap(openFileDialog.FileName);
-                        //imageArea.ReplaceImageArea(imageArea.GetFrame(), fileData, bitmap.Width, bitmap.Height);
-                        imageArea.ReplaceImageArea(imageArea.GetFrame(), openFileDialog.FileName,string.Empty);
+                        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;
+                        }
+                        
+                        imageArea.ReplaceImageArea(imageArea.GetFrame(), imageData, imageWidth, imageHeight);
+                        //imageArea.ReplaceImageArea(imageArea.GetFrame(), openFileDialog.FileName,string.Empty);
                         pdfContentEditControl.RefreshThumb();
                         UpdateImageArea(imageArea);
                     }