Ignacio Soler Garcia
Ignacio Soler Garcia

Reputation: 21855

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:

enter image description here

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.

Upvotes: 0

Views: 272

Answers (2)

Tim_Mac
Tim_Mac

Reputation: 166

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.

Upvotes: 1

Mahender
Mahender

Reputation: 5664

Remove the second using statement for stream object which will fix the issue.

Upvotes: 0

Related Questions