private static BitmapImage CreateImage(byte[] b, int dpw, int dph) { try { using (var ms = new MemoryStream(b, false)) using (var ws = new WrappingStream(ms)) { var bi = new BitmapImage(); bi.BeginInit(); bi.CacheOption = BitmapCacheOption.OnLoad; bi.StreamSource = ws; if (dpw > 0 || dph > 0) { bi.DecodePixelWidth = dpw; bi.DecodePixelHeight = dph; } bi.EndInit(); bi.Freeze(); return bi; } } catch { return null; } }
private void SDK_LiveViewUpdated(Stream img) { try { if (CameraHandler.IsLiveViewOn) { using (WrappingStream s = new WrappingStream(img)) { img.Position = 0; BitmapImage EvfImage = new BitmapImage(); EvfImage.BeginInit(); EvfImage.StreamSource = s; EvfImage.CacheOption = BitmapCacheOption.OnLoad; EvfImage.EndInit(); EvfImage.Freeze(); Application.Current.Dispatcher.Invoke(SetImageAction, EvfImage); } } } catch (Exception ex) { ReportError(ex.Message, false); } }
private static void UriSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { var img = sender as LazyImage; if (img == null) return; var dpw = img.DecodePixelWidth; var dph = img.DecodePixelHeight; var uri = e.NewValue as Uri; if (e.NewValue == e.OldValue || uri == null) return; try { if (uri.Scheme == "pack") { // PACK image var bi = new BitmapImage(uri) { CacheOption = BitmapCacheOption.OnLoad }; bi.Freeze(); SetImage(img, bi, uri); } else { img.Source = null; Subject<byte[]> publisher = null; _imageStreamer.GetOrAdd(uri, _ => publisher = new Subject<byte[]>()) .Select(b => { try { using (var ms = new MemoryStream(b, false)) using (var ws = new WrappingStream(ms)) { var bi = new BitmapImage(); bi.BeginInit(); bi.CacheOption = BitmapCacheOption.OnLoad; bi.StreamSource = ws; if (dpw > 0 || dph > 0) { bi.DecodePixelWidth = dpw; bi.DecodePixelHeight = dph; } bi.EndInit(); bi.Freeze(); return bi; } } catch { return null; } }) .Subscribe(b => DispatcherHolder.Enqueue( () => SetImage(img, b, uri), DispatcherPriority.Loaded) , ex => { }); if (publisher != null) { _taskFactory.StartNew(() => LoadBytes(uri, publisher)); } } } // ReSharper disable EmptyGeneralCatchClause catch // ReSharper restore EmptyGeneralCatchClause { } }
private void SDK_LiveViewUpdated(Stream img) { if (CameraHandler.IsLiveViewOn) { using (WrappingStream s = new WrappingStream(img)) { img.Position = 0; EvfImage = new BitmapImage(); EvfImage.BeginInit(); EvfImage.StreamSource = s; EvfImage.CacheOption = BitmapCacheOption.OnLoad; EvfImage.EndInit(); EvfImage.Freeze(); Application.Current.Dispatcher.Invoke(SetImageAction); } } }