Out of memory exception while loading images from isolated storage

爷,独闯天下 提交于 2019-12-12 09:17:43

问题


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:


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

            }


来源:https://stackoverflow.com/questions/15700340/out-of-memory-exception-while-loading-images-from-isolated-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!