Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.OutOfMemoryException while reading and binding image from isolated storage

This is the code I am using for binding image in XAML

               <Border  toolkit:TiltEffect.IsTiltEnabled="true" Height="350" Width="400" Grid.ColumnSpan="3">
                        <Grid  Height="350" Width="400" Margin="70,0,70,0" x:Name="Container1">
                            <Grid.Background>
                                <ImageBrush ImageSource="{Binding ImageCollection[0]}" Stretch="Uniform" AlignmentX="Left" AlignmentY="Center"/>
                            </Grid.Background>
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Tap">
                                    <i:InvokeCommandAction Command="{Binding ImageTapCommand}" CommandParameter="CONTAINER0"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </Grid>
                    </Border>

Likewise I am using 4 border for displaying my recent images.

In my ViewModel I am using the below method for reading image from isolated storage.

 public Stream GetFileStream(string filename, ImageLocation location)
    {
        try
        {
            lock (SyncLock)
            {
                if (location == ImageLocation.RecentImage)
                {
                    filename = Constants.IsoRecentImage + @"\" + filename;
                }
                using (var iSf = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!iSf.FileExists(filename)) return null;
                    var fs = iSf.OpenFile(filename, FileMode.Open, FileAccess.Read);
                    return fs;
                }
            }

        }
        catch (Exception ex)
        {
                 return null;
        }

    }

And after getting the stream I will use this below written method four building the WritableBitmap for UI binding

     private WriteableBitmap BuildImage(Stream imageStream)
    {
        using (imageStream)
        {
            var image = new BitmapImage();
            image.SetSource(imageStream);
            return new WriteableBitmap(image);
        }
    }

In this case my issue is after navigating to and from my page for two - three times. The app crashes on BuildImage() method where I am using " image.SetSource(imageStream);" method. I tried many alternatives but failed. The exception I am getting is "System.OutOfMemoryException "

I tried Image control instead of Image brush.

I tried Bitmap instead of WritableBitmap etc. but the result is same.

The app crashing rate will reduce if I use small images. But the crash rate is high with the images taken through camera.

I am trying for a solution to this issue for last one week, But didn't find any alternative to fix the issue.

I found a link that talks about similar issue But didn't get many to solve the issue

like image 665
StezPet Avatar asked Apr 24 '13 07:04

StezPet


3 Answers

Try this, 

  var bitmapImage = new BitmapImage();
  bitmapImage.SetSource(stream);
  bitmapImage.CreateOptions = BitmapCreateOptions.None;
  var bmp = new WriteableBitmap((BitmapSource) bitmapImage);
  bitmapImage.UriSource = (Uri) null;
  return bmp;
like image 65
Stephan Ronald Avatar answered Nov 20 '22 20:11

Stephan Ronald


Silverlight caches images by default to improve performance. You should call image.UriSource = null after using the BitmapImage to dispose of the resources.

like image 42
pantaloons Avatar answered Nov 20 '22 22:11

pantaloons


Are you reseting/disposing the IsolatedStorageFileStream and the IsolatedStorageFile after you use them?

like image 1
r wank Avatar answered Nov 20 '22 22:11

r wank