public class IsoStoreImageSource : DependencyObject { public static void SetIsoStoreFileName(UIElement element,string value) { element.SetValue(IsoStoreFileNameProperty,value); } public static string GetIsoStoreFileName(UIElement element) { return (string)element.GetValue(IsoStoreFileNameProperty); } // Using a DependencyProperty as the backing store for IsoStoreFileName. This enables animation,styling,binding,etc... public static readonly DependencyProperty IsoStoreFileNameProperty = DependencyProperty.Registerattached("IsoStoreFileName",typeof(string),typeof(IsoStoreImageSource),new PropertyMetadata("",Changed)); private static void Changed(DependencyObject d,DependencyPropertyChangedEventArgs e) { Image img = d as Image; if (img != null) { var path = e.NewValue as string; SynchronizationContext uiThread = SynchronizationContext.Current; Task.Factory.StartNew(() => { using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication()) { if (isoStore.FileExists(path)) { var stream = isoStore.OpenFile(path,System.IO.FileMode.Open,FileAccess.Read); uiThread.Post(_ => { var _img = new BitmapImage(); _img.SetSource(stream); img.source = _img; },null); } } }); } }
}
我在ListBox控件中使用它.如果尝试使用默认库图像,一切都将按预期工作.但是,如果我尝试使用大尺寸的图像(通过设备相机拍摄),应用程序会崩溃.
这是我得到的例外
System.Windows.ni.dll中出现“System.OutOfMemoryException”类型的异常,但未在用户代码中处理
堆栈跟踪
在MS.Internal.FrameworkCallbacks.NotifyManagedDebuggerOnNativeOOM()
在MS.Internal.XcpImports.BitmapSource_SetSource(BitmapSource bitmapSource,CValue& byteStream)
在System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(Stream streamSource)
在System.Windows.Media.Imaging.BitmapImage.SetSourceInternal(Stream streamSource)
在System.Windows.Media.Imaging.BitmapSource.SetSource(Stream streamSource)
在MyaPP.Common.IsoStoreImageSource.<> c__displayClass4.<> c__displayClass6.b__1(Object _)
if (img != null) { BitmapImage bitmapImage = img.source as BitmapImage; bitmapImage.UriSource = null; img.source = null; //rest of the code ... }
原文地址:https://www.jb51.cc/windows/364909.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。