Out of memory exception while loading images from

2019-04-16 15:47发布

I am getting OutofMemoryException in this particular code.

public BitmapImage GetImage(int pageNo)
        {
            if (!this._isLoaded)
            {
                this.Load();
            }
            using (IsolatedStorageFileStream stream = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(this.FileNames[pageNo], FileMode.Open, FileAccess.Read))
            {
                BitmapImage image = new BitmapImage();
                image.SetSource(stream);            

                return image;

            }
        }

The out of memory exception is occuring at image.SetSource(stream) . I cant set the uri to null because I have to return the image.

What is the workaround for this? Help me here.

1条回答
Fickle 薄情
2楼-- · 2019-04-16 16:14

I had this list of bitmap images.

 private List<BitmapImage> _images = new List<BitmapImage>();

I cleared the uri while leaving the page.

 protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            this.DataContext = null;
            foreach (var obj in this._images)
            {
                if (obj != null)
                {
                    obj.ClearValue(BitmapImage.UriSourceProperty);
                }

            }
查看更多
登录 后发表回答