|
@@ -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();
|