Application error while trying to mount an isolated storage image

In my application, I use the helper method described below to bind my isolated storage image to image management. I got this helper method from the link " Linking an image stored in isolated storage to manage images on Windows Phone "

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); } } }); } } 

}

I use this inside a ListBox control. And if you try with the default library images, everything will work as expected. But if I try with images with a large size (taken through the device’s camera), the application will fail.

And so, except that I get

An exception of type "System.OutOfMemoryException" occurred in System.Windows.ni.dll, but was not processed in the user code

stack trace

in MS.Internal.FrameworkCallbacks.NotifyManagedDebuggerOnNativeOOM () in MS.Internal.XcpImports.BitmapSource_SetSource (BitmapSource bitmapSource, CValue & byteStream) in System.Windows.Media.Imaging.BitmapSource.Set.ourceSource.etource.Set.stream.Source.etource.Set.stream.ource Stream .BitmapImage.SetSourceInternal (stream streamSource) in System.Windows.Media.Imaging.BitmapSource.SetSource (stream streamSource) on MyaPP.Common.IsoStoreImageSource. <> c__DisplayClass4. <> c__DisplayClass6.b__1 (Object _)

+8
windows-phone windows-phone-7 windows-phone-8
source share
3 answers

Caching inside the ListBox can take up your memory, and this is especially noticeable when using large images. I am not familiar with the helper method that you posted, but try adding this.

 if (img != null) { BitmapImage bitmapImage = img.Source as BitmapImage; bitmapImage.UriSource = null; img.Source = null; //rest of the code ... } 
0
source share

Well, it took me a while to get back to this issue. I will share my findings here, but I do not see them as a real answer to this question, but rather a workaround. However, I hope this helps someone.

First, I want to confirm that an OutOfMemoryException occurs in certain circumstances. But, surprisingly, it depends on the page layout you use. In fact, if your layout includes a StackPanel , you will get an exception. I suppose it comes down to how the MeasureOverride and ArrangeOverride implemented in the StackPanel (although I may be completely wrong here). It appears that when the ListBox is a child of the StackPanel , it tries to load all the images before displaying it. This, of course, causes a memory leak.

On the other hand, if you use something like a Grid as a parent for a list of images, then there are no such exceptions, and memory loading is reasonable.

Here is the page layout that worked for me:

 <Grid> <ListBox ItemsSource="{Binding IsoStorePics}"> <ListBox.ItemTemplate> <DataTemplate> <Image local:IsoStoreImageSource.IsoStoreFileName="{Binding Path}" Margin="5"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> 

This is the best answer I have for you now. Please let me know if this helps.

0
source share

You can try like this, the Stream object will be automatically deleted.

 using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication()) { if (iso.FileExists(imagePath)) { using (Stream imagestream = new IsolatedStorageFileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, iso)) { BitmapImage bmp = new BitmapImage(); bmp.SetSource(imagestream); imgControl.Source = bmp; } } } 
0
source share

All Articles