public async Task <BitmapImage> ToBitmapImage(SoftwareBitmap softwareBitmap) { using (var bitmapImageStream = new InMemoryRandomAccessStream()) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, bitmapImageStream); encoder.SetSoftwareBitmap(softwareBitmap); await encoder.FlushAsync(); var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = bitmapImageStream.AsStreamForRead(); bitmapImage.EndInit(); bitmapImage.Freeze(); return(bitmapImage); } }
public ImageForm(IVisualizerObjectProvider objectProvider) { InitializeComponent(); #if DEBUG ShowInTaskbar = true; #endif label1.Font = UIFont; SetFontAndScale(label1, UIFont); label2.Font = UIFont; txtExpression.Font = UIFont; CloseB.Font = UIFont; object objectBitmap = objectProvider.GetObject(); object image = null; if (objectBitmap != null) { if (objectBitmap is ByteArraySerializableWrapper <SoftwareBitmap> wrapper) { image = Serializer.ToSoftwareBitmap(wrapper.Bytes); } #if DEBUG string expression = objectBitmap.ToString(); #endif var method = objectBitmap.GetType().GetMethod("ToBitmap", new Type[] { }); if (method != null) { objectBitmap = method.Invoke(objectBitmap, null); } System.Windows.Media.ImageSource bitmapSource = null; if (objectBitmap is Bitmap) { image = objectBitmap; //var hObject = ((Bitmap)objectBitmap).GetHbitmap(); //try //{ // bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( // hObject, // IntPtr.Zero, // Int32Rect.Empty, // BitmapSizeOptions.FromEmptyOptions()); // image = bitmapSource; //} //catch (Win32Exception) //{ // bitmapSource = null; //} //finally //{ // DeleteObject(hObject); //} } else if (objectBitmap is SerializableBitmapImage serializableBitmapImage) { if (serializableBitmapImage.Image != null) { bitmapSource = (System.Windows.Media.Imaging.BitmapSource)serializableBitmapImage; } else if (serializableBitmapImage.SoftwareBitmap != null) { using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { Windows.Graphics.Imaging.BitmapEncoder encoder = Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId, stream).GetResults(); encoder.SetSoftwareBitmap(serializableBitmapImage.SoftwareBitmap); var t = Task.Run(async() => await encoder.FlushAsync()); t.GetAwaiter().GetResult(); var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.StreamSource = stream.AsStream(); bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.EndInit(); bitmap.Freeze(); bitmapSource = bitmap; image = (ImageSource)bitmap; // use bitmap } //using (var sb = serializableBitmapImage.SoftwareBitmap.LockBuffer()) //{ // var source = WriteableBitmap.Create(serializableBitmapImage.SoftwareBitmap.PixelWidth, serializableBitmapImage.SoftwareBitmap.PixelHeight, serializableBitmapImage.SoftwareBitmap.DpiX, serializableBitmapImage.SoftwareBitmap.DpiY, // serializableBitmapImage.SoftwareBitmap.BitmapPixelFormat, null, sb.CreateReference()., serializableBitmapImage.SoftwareBitmap.PixelWidth); // source. //} // var bs = new SoftwareBitmapSource(); // await bs.SetBitmapAsync(serializableBitmapImage.SoftwareBitmap); //bitmapSource = bs; } } //if (bitmapSource != null) //{ // imageControl.SetImage(bitmapSource); //} var type = image.GetType(); Func <ImageUserControl> factory = null; while (type != null && !ImageControls.TryGetValue(type, out factory)) { type = type.BaseType; } if (factory is { })