To .NET 5 (lot of refactoring)
[master-thesis.git] / Parasitemia / ParasitemiaUI / BitmapSourceConvert.fs
diff --git a/Parasitemia/ParasitemiaUI/BitmapSourceConvert.fs b/Parasitemia/ParasitemiaUI/BitmapSourceConvert.fs
new file mode 100644 (file)
index 0000000..ea41240
--- /dev/null
@@ -0,0 +1,30 @@
+namespace ParasitemiaUI
+
+open System
+open System.Runtime.InteropServices
+open System.Windows
+open System.Windows.Media.Imaging
+
+open Emgu.CV
+
+module BitmapSourceConvert =
+    [<DllImport("gdi32")>]
+    extern int DeleteObject(IntPtr o);
+
+    let ToBitmapSource (image : Image<'TColor, 'TDepth>) : BitmapSource =
+        use source = image.ToBitmap ()
+
+        let ptr : IntPtr = source.GetHbitmap ()
+
+        let bs : BitmapSource =
+            System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap
+                (
+                    ptr,
+                    IntPtr.Zero,
+                    Int32Rect.Empty,
+                    (BitmapSizeOptions.FromEmptyOptions ())
+                )
+
+        DeleteObject ptr |> ignore
+
+        bs