Browse Source

compdfkit(win) - 修复数字签名无法添加图片问题

liuaoran 1 year ago
parent
commit
0906625030

+ 122 - 7
Demo/Examples/Compdfkit_Tools/DigitalSignature/FillDigitalSignatureControl/FillDigitalSignatureControl.xaml.cs

@@ -11,6 +11,7 @@ using System.Drawing;
 using System.Drawing.Imaging;
 using System.IO;
 using System.Reflection;
+using System.Runtime.InteropServices;
 using System.Runtime.Remoting.Messaging;
 using System.Text;
 using System.Windows;
@@ -136,12 +137,10 @@ namespace Compdfkit_Tools.PDFControl
                             tempSignatureConfig.LogoData = imageArray;
                             tempSignatureConfig.LogoWidth = ImageWidth;
                             tempSignatureConfig.LogoHeight = ImageHeight;
-                            //signatureWidget.UpdateApWithImage(ImageArray, ImageWidth, ImageHeight, C_Scale_Type.fitCenter, 0);
                         }
                     }
                 }
                 
-                //tempSignatureConfig.LogoData = CommonHelper.ConvertBitmapToByteArray(new Bitmap(logoPath));
             }
             tempSignatureConfig.Content = Text;
             tempSignatureConfig.TextColor = textColor;
@@ -237,7 +236,28 @@ namespace Compdfkit_Tools.PDFControl
                         ImagePickPanel.Visibility = Visibility.Visible;
                         if (!string.IsNullOrEmpty(imagePath))
                         {
-                            //tempSignatureConfig.ImageBitmap = new Bitmap(imagePath);
+                            using (FileStream fileData = File.OpenRead(imagePath))
+                            {
+                                BitmapFrame frame = null;
+                                BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
+                                if (decoder != null && decoder.Frames.Count > 0)
+                                {
+                                    frame = decoder.Frames[0];
+                                }
+                                if (frame != null)
+                                {
+                                    byte[] imageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
+                                    int ImageWidth = frame.PixelWidth;
+                                    int ImageHeight = frame.PixelHeight;
+                                    frame.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
+                                    if (signatureWidget.IsValid())
+                                    {
+                                        tempSignatureConfig.ImageData = imageArray;
+                                        tempSignatureConfig.ImageWidth = ImageWidth;
+                                        tempSignatureConfig.ImageHeight = ImageHeight; 
+                                    }
+                                }
+                            } 
                         }
                     }
                 }
@@ -280,13 +300,17 @@ namespace Compdfkit_Tools.PDFControl
 
         private void CanvasPopupConfirm_Click(object sender, RoutedEventArgs e)
         {
-            //tempSignatureConfig.ImageBitmap = GetDrawInk();
+            int height = 0;
+            int width = 0;
+            tempSignatureConfig.ImageData = GetDrawInk(ref height,ref width);
+            tempSignatureConfig.ImageWidth = width;
+            tempSignatureConfig.ImageHeight = height;
             CanvaDrawPopup.Visibility = Visibility.Collapsed;
             SetProperty();
             CreateTempSignature();
         }
 
-        public Bitmap GetDrawInk()
+        public byte[] GetDrawInk(ref int height, ref int width)
         {
             if (DrawInkCanvas != null && DrawInkCanvas.Strokes != null && DrawInkCanvas.Strokes.Count > 0)
             {
@@ -332,7 +356,10 @@ namespace Compdfkit_Tools.PDFControl
                 using (MemoryStream newStream = new MemoryStream())
                 {
                     pngEncoder.Save(newStream);
-                    return new Bitmap(newStream);
+                    Bitmap bitmap = new Bitmap(newStream);
+                    height = bitmap.Height;
+                    width = bitmap.Width;
+                    return GetBitmapData(bitmap);
                 }
             }
             else
@@ -341,6 +368,29 @@ namespace Compdfkit_Tools.PDFControl
             }
         }
 
+        private byte[] GetBitmapData(Bitmap bitmap)
+        {
+            // Lock the bitmap's bits.
+            Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
+            BitmapData bmpData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, bitmap.PixelFormat);
+
+            // Get the address of the first line.
+            IntPtr ptr = bmpData.Scan0;
+
+            // Declare an array to hold the bytes of the bitmap.
+            int bytes = Math.Abs(bmpData.Stride) * bitmap.Height;
+
+            byte[] rgbValues = new byte[bytes];
+
+            // Copy the RGB values into the array.
+            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
+
+            // Unlock the bits.
+            bitmap.UnlockBits(bmpData);
+
+            return rgbValues;
+        }
+        
         private void SetProperty()
         {
             Text = string.Empty;
@@ -455,6 +505,53 @@ namespace Compdfkit_Tools.PDFControl
             ReasonPanel.Visibility = checkItem.IsChecked == true ? Visibility.Visible : Visibility.Collapsed;
         }
 
+        internal static Bitmap ConvertToBitmapARGB(Bitmap bitmap)
+        {
+            try
+            {
+                if (bitmap == null || bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb)
+                {
+                    return bitmap;
+                }
+                using (MemoryStream streamData = new MemoryStream())
+                {
+                    bitmap.Save(streamData, ImageFormat.Png);
+                    BitmapDecoder decoder = BitmapDecoder.Create(streamData, BitmapCreateOptions.None, BitmapCacheOption.Default);
+                    if (decoder != null && decoder.Frames.Count > 0)
+                    {
+                        BitmapFrame frame = decoder.Frames[0];
+                        byte[] imageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
+                        if (frame.Format != PixelFormats.Bgra32)
+                        {
+                            FormatConvertedBitmap formatCovert = new FormatConvertedBitmap(frame, PixelFormats.Bgra32, frame.Palette, 0);
+                            formatCovert.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
+                        }
+                        else
+                        {
+                            frame.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
+                        }
+
+                        WriteableBitmap writeBitmap = new WriteableBitmap(frame.PixelWidth, frame.PixelHeight, 96, 96, PixelFormats.Bgra32, null);
+                        writeBitmap.WritePixels(new Int32Rect(0, 0, frame.PixelWidth, frame.PixelHeight), imageArray, frame.PixelWidth * 4, 0);
+                        BitmapFrame newFrame = BitmapFrame.Create(writeBitmap);
+                        PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
+                        pngEncoder.Frames.Add(newFrame);
+                        using (MemoryStream newStream = new MemoryStream())
+                        {
+                            pngEncoder.Save(newStream);
+                            return new Bitmap(newStream);
+                        }
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+
+            }
+
+            return null;
+        }
+
         private void BrowseTxt_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
         {
             string filter = "Image files (*.jpg, *.jpeg, *.png, *.bmp)|*.jpg;*.jpeg;*.png;*.bmp";
@@ -464,7 +561,25 @@ namespace Compdfkit_Tools.PDFControl
                 imagePath = pngPath;
                 try
                 {
-                    //tempSignatureConfig.ImageBitmap = new Bitmap(imagePath);
+                    Bitmap bitmap = new Bitmap(imagePath);
+                    Bitmap newImageBitmap = null;
+                    BitmapData bitmapData = null;
+                    IntPtr bitmapPtr = IntPtr.Zero;
+                    if (bitmap != null)
+                    {
+                        newImageBitmap = ConvertToBitmapARGB(bitmap);
+                        Rectangle rect = new Rectangle(0, 0, newImageBitmap.Width, newImageBitmap.Height);
+                        bitmapData = newImageBitmap.LockBits(rect, ImageLockMode.ReadOnly, newImageBitmap.PixelFormat);
+                        bitmapPtr = bitmapData.Scan0;
+                        int bytes = Math.Abs(bitmapData.Stride) * bitmap.Height;
+                        byte[] rgbValues = new byte[bytes];
+                         
+                        Marshal.Copy(bitmapData.Scan0, rgbValues, 0, bytes);
+                        tempSignatureConfig.ImageData = rgbValues;
+                        tempSignatureConfig.ImageWidth = bitmapData.Width;
+                        tempSignatureConfig.ImageHeight = bitmapData.Height;
+
+                    }
                 }
                 catch (Exception exception)
                 {