Преглед изворни кода

Fix to empty bitmapimage problems by hiding a method and making anyone go through the ImageUtils.LoadImage(byte[]?) function, allowing the result to be null.

Kenric Nugteren пре 1 година
родитељ
комит
713802edcd

+ 2 - 2
inabox.wpf/Converters/BytesToBitmapImageConverter.cs

@@ -2,8 +2,8 @@ using System.Windows.Media.Imaging;
 
 
 namespace InABox.WPF;
 namespace InABox.WPF;
 
 
-public class BytesToBitmapImageConverter : AbstractConverter<byte[], BitmapImage>
+public class BytesToBitmapImageConverter : AbstractConverter<byte[]?, BitmapImage?>
 {
 {
-    public override BitmapImage Convert(byte[] value)
+    public override BitmapImage? Convert(byte[]? value)
         => ImageUtils.LoadImage(value);
         => ImageUtils.LoadImage(value);
 }
 }

+ 23 - 23
inabox.wpf/DigitalForms/Designer/Controls/DFImageControl.cs

@@ -12,34 +12,34 @@ using System.Windows.Controls;
 using System.Windows.Media;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using System.Windows.Media.Imaging;
 
 
-namespace InABox.DynamicGrid
+namespace InABox.DynamicGrid;
+
+public class DFImageControl : DynamicFormControl<DFLayoutImage>
 {
 {
-    public class DFImageControl : DynamicFormControl<DFLayoutImage>
+    private static readonly Dictionary<Guid, BitmapImage?> images = new();
+    protected override FrameworkElement Create()
     {
     {
-        private static readonly Dictionary<Guid, BitmapImage> images = new();
-        protected override FrameworkElement Create()
+        var image = new Image();
+        if (Control.Image.IsValid())
         {
         {
-            var image = new Image();
-            if (Control.Image.IsValid())
+            if (images.TryGetValue(Control.Image.ID, out var bitmapImage))
             {
             {
-                if (images.ContainsKey(Control.Image.ID)) image.Source = images[Control.Image.ID];
-                new Client<Document>().Query(
-                    new Filter<Document>(x => x.ID).IsEqualTo(Control.Image.ID),
-                    new Columns<Document>(x => x.ID, x => x.Data),
-                    null,
-                    (data, error) =>
-                    {
-                        var bytes = data?.Rows.FirstOrDefault()?.Get<Document, byte[]>(x => x.Data);
-                        var source = new BitmapImage();
-                        source.LoadImage(bytes);
-                        images[Control.Image.ID] = source;
-                        Dispatcher.Invoke(() => { image.Source = source; });
-                    }
-                );
+                image.Source = bitmapImage;
             }
             }
-
-            image.Stretch = Stretch.Uniform;
-            return image;
+            new Client<Document>().Query(
+                new Filter<Document>(x => x.ID).IsEqualTo(Control.Image.ID),
+                new Columns<Document>(x => x.ID, x => x.Data),
+                null,
+                (data, error) =>
+                {
+                    var source = ImageUtils.LoadImage(data?.Rows.FirstOrDefault()?.Get<Document, byte[]>(x => x.Data));
+                    images[Control.Image.ID] = source;
+                    Dispatcher.Invoke(() => { image.Source = source; });
+                }
+            );
         }
         }
+
+        image.Stretch = Stretch.Uniform;
+        return image;
     }
     }
 }
 }

+ 6 - 5
inabox.wpf/DigitalForms/Designer/Controls/Fields/DFEmbeddedImageControl.cs

@@ -230,12 +230,13 @@ namespace InABox.DynamicGrid
             return _grid;
             return _grid;
         }
         }
 
 
-        private void ShowImage(byte[] data)
+        private static void ShowImage(byte[] data)
         {
         {
-            var src = new BitmapImage();
-            src.LoadImage(data);
-            SfImageEditor editor = new SfImageEditor();
-            editor.ImageSource = src;
+            var src = ImageUtils.LoadImage(data);
+            var editor = new SfImageEditor
+            {
+                ImageSource = src
+            };
             var window = new Window() { Content = editor, Height = 800, Width=800 };
             var window = new Window() { Content = editor, Height = 800, Width=800 };
             window.ShowDialog();
             window.ShowDialog();
         }
         }

+ 1 - 3
inabox.wpf/DynamicGrid/PDF/ImageEditorControl.xaml.cs

@@ -107,9 +107,7 @@ namespace InABox.DynamicGrid
                     documentdata = File.ReadAllBytes(cachefile);
                     documentdata = File.ReadAllBytes(cachefile);
                 }
                 }
 
 
-                var img = new BitmapImage();
-                img.LoadImage(documentdata);
-                Editor.ImageSource = img;
+                Editor.ImageSource = ImageUtils.LoadImage(documentdata);
             }
             }
         }
         }
 
 

+ 11 - 7
inabox.wpf/ImageUtils.cs

@@ -377,20 +377,24 @@ namespace InABox.WPF
             }
             }
         }
         }
 
 
-        public static BitmapImage LoadImage(byte[] imageData)
+        /// <summary>
+        /// Load an image from image data; this will return <see langword="null"/> if <paramref name="imageData"/> is <see langword="null"/> or empty.
+        /// </summary>
+        /// <param name="imageData"></param>
+        /// <returns></returns>
+        public static BitmapImage? LoadImage(byte[]? imageData)
         {
         {
+            if(imageData is null || imageData.Length == 0)
+            {
+                return null;
+            }
             var result = new BitmapImage();
             var result = new BitmapImage();
             result.LoadImage(imageData);
             result.LoadImage(imageData);
             return result;
             return result;
         }
         }
 
 
-        public static void LoadImage(this BitmapImage image, byte[]? imageData)
+        private static void LoadImage(this BitmapImage image, byte[] imageData)
         {
         {
-            if (imageData == null || imageData.Length == 0)
-            {
-                return;
-            }
-
             using (var mem = new MemoryStream(imageData))
             using (var mem = new MemoryStream(imageData))
             {
             {
                 mem.Position = 0;
                 mem.Position = 0;