简体   繁体   中英

Weird ArgumentException using IsolatedStorageFile and BitmapImage

I'm using the following code to read some cached images from an IsolatedStorageFile:

using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream stream = storageFile.OpenFile(filename, FileMode.Open, FileAccess.Read))
    {
        BitmapImage result = new BitmapImage();
        result.SetSource(stream);
        return result;
    }
}

From time to time I get the following exception:

在此处输入图片说明

The detail has the following text:

Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection

Any ideas? I don't know if this matters but this code is accessed in a heavy multithreaded environment (I mean that there are a lot of threads calling these lines).

I know this is a first chance exception but anyway I don't get the reason.

I have encountered the same error with very similar code. in my experience it only happens on a corrupt photo file. the photos are all generated by the CameraCaptureTask and i have never been able to reproduce the error, but it happens in the wild.

the error message is a bit misleading.

i'm explicitly calling Close+Dispose on the filestream but that is equivalent to the "using" declarations, ignacio is correct in his reply above.

删除流对象的第二个using语句,这将解决此问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM