Browse Source

WPF - fix to Digital Forms paste from clipboard not saving image

Nick-PRSDigital@bitbucket.org 2 years ago
parent
commit
eb05b97a0f

+ 18 - 3
inabox.wpf/DigitalForms/Designer/Controls/Fields/DFEmbeddedImageControl.cs

@@ -15,6 +15,7 @@ using Image = System.Windows.Controls.Image;
 using System.Diagnostics.CodeAnalysis;
 using System.IO;
 using Microsoft.Win32;
+using System.Drawing.Imaging;
 
 namespace InABox.DynamicGrid
 {
@@ -108,6 +109,8 @@ namespace InABox.DynamicGrid
                 return false;
             }
         }
+
+        
     }
 
     public class DFEmbeddedImageControl : DynamicFormFieldControl<DFLayoutEmbeddedImage, DFLayoutEmbeddedImageProperties, byte[]>
@@ -175,9 +178,21 @@ namespace InABox.DynamicGrid
         {
             if (Clipboard.ContainsImage())
             {
-                var img = Clipboard.GetImage();
-                Image.Source = img;
-                ChangeField();
+                var paste = Clipboard.GetImage();
+                var bitmap = ImageUtils.BitmapSourceToBitmap(paste);
+                var img = ImageUtils.ToBitmapImage(bitmap);
+                var empty = img == null;
+                if (_isEmpty != empty)
+                {
+                    _isEmpty = empty;
+
+                    if (empty)
+                        Image.Source = EmbeddedImageUtilities.CreateEmptyImage();
+                    else
+                        Image.Source = img;
+
+                    ChangeField();
+                }
             }
         }
 

+ 52 - 0
inabox.wpf/ImageUtils.cs

@@ -316,6 +316,58 @@ namespace InABox.WPF
             }
         }
 
+        public static Bitmap BitmapSourceToBitmap(BitmapSource source)
+        {
+            if (source == null)
+                return null;
+
+            var pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;  //Bgr32 equiv default
+            if (source.Format == PixelFormats.Bgr24)
+                pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
+            else if (source.Format == PixelFormats.Pbgra32)
+                pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppPArgb;
+            else if (source.Format == PixelFormats.Prgba64)
+                pixelFormat = System.Drawing.Imaging.PixelFormat.Format64bppPArgb;
+
+            Bitmap bmp = new Bitmap(
+                source.PixelWidth,
+                source.PixelHeight,
+                pixelFormat);
+
+            BitmapData data = bmp.LockBits(
+                new Rectangle(System.Drawing.Point.Empty, bmp.Size),
+                ImageLockMode.WriteOnly,
+                pixelFormat);
+
+            source.CopyPixels(
+                Int32Rect.Empty,
+                data.Scan0,
+                data.Height * data.Stride,
+                data.Stride);
+
+            bmp.UnlockBits(data);
+
+            return bmp;
+        }
+
+        public static BitmapImage ToBitmapImage(this Bitmap bitmap)
+        {
+            using (var memory = new MemoryStream())
+            {
+                bitmap.Save(memory, ImageFormat.Png);
+                memory.Position = 0;
+
+                var bitmapImage = new BitmapImage();
+                bitmapImage.BeginInit();
+                bitmapImage.StreamSource = memory;
+                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
+                bitmapImage.EndInit();
+                bitmapImage.Freeze();
+
+                return bitmapImage;
+            }
+        }
+
         public static BitmapImage LoadImage(byte[] imageData)
         {
             var result = new BitmapImage();