简体   繁体   English

从IsolatedStorage加载保存的图像时发生System.InvalidOperationException

[英]System.InvalidOperationException occur when load saved Image from IsolatedStorage

I have found a class from the link ImageCaching but when i load from isolated storage i got an exeption "System.InvalidOperationException" 我从链接ImageCaching中找到了一个类,但是当我从隔离存储中加载时,我得到了一个例外“ System.InvalidOperationException”

here is my code 这是我的代码

 public static object DownloadFromWeb(Uri imageFileUri)
    {
        WebClient m_webClient = new WebClient();                                //Load from internet
        BitmapImage bm = new BitmapImage();

        m_webClient.OpenReadCompleted += (o, e) =>
        {
            if (e.Error != null || e.Cancelled) return;
            WriteToIsolatedStorage(IsolatedStorageFile.GetUserStoreForApplication(), e.Result, GetFileNameInIsolatedStorage(imageFileUri));
            bm.SetSource(e.Result);
            e.Result.Close();
        };
        m_webClient.OpenReadAsync(imageFileUri);
        return bm;
    }

    public static object ExtractFromLocalStorage(Uri imageFileUri)
    {
        byte[] data;
        string isolatedStoragePath = GetFileNameInIsolatedStorage(imageFileUri);       //Load from local storage
        if (null == _storage)
        {
             _storage = IsolatedStorageFile.GetUserStoreForApplication();
        }
        using (IsolatedStorageFileStream sourceFile = _storage.OpenFile(isolatedStoragePath, FileMode.Open, FileAccess.Read))
        {


            // Read the entire file and then close it
            sourceFile.Read(data, 0, data.Length);
            sourceFile.Close();
           BitmapImage bm = new BitmapImage();
            bm.SetSource(sourceFile);///here got the exeption 
            return bm;

        }

    }

so that i can't set the image. 这样我就不能设置图像。

I'm using the converter you mentioned and it works but you modified the method. 我正在使用您提到的转换器,它可以工作,但是您修改了方法。

Your ExtractFromLocalStorage method isn't the same. 您的ExtractFromLocalStorage方法不相同。 You close your stream before to use it with the SetSource method. 关闭流,然后将其与SetSource方法一起使用。

Here is the original method code: 这是原始的方法代码:

 private static object ExtractFromLocalStorage(Uri imageFileUri)
    {
        string isolatedStoragePath = GetFileNameInIsolatedStorage(imageFileUri);       //Load from local storage
        using (var sourceFile = _storage.OpenFile(isolatedStoragePath, FileMode.Open, FileAccess.Read))
        {
            BitmapImage bm = new BitmapImage();
            bm.SetSource(sourceFile);
            return bm;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从WP8中的xml读取时出现System.InvalidOperationException - System.InvalidOperationException when reading from xml in WP8 调用WCF服务时出现System.InvalidOperationException - System.InvalidOperationException when calling WCF Service 尝试 .ThenInculde 时出现 System.InvalidOperationException - System.InvalidOperationException when attempting `.ThenInculde` 填充数组时出现System.InvalidOperationException错误 - System.InvalidOperationException error when populating an array System.InvalidOperationException:提交消息时 - System.InvalidOperationException: When submitting a message System.InvalidOperationException:访问DbContext时 - System.InvalidOperationException: when accessing DbContext 尝试保存在数据库中时 System.InvalidOperationException - System.InvalidOperationException when trying to save in database 管理对话框时出现System.InvalidOperationException - System.InvalidOperationException when managing a dialog 使用ApiController获取列表时出现System.InvalidOperationException - System.InvalidOperationException when getting a List with an ApiController 如何从RhinoMocks解决System.InvalidOperationException? - How to solve System.InvalidOperationException from RhinoMocks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM