Reputation: 21855
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.
Upvotes: 0
Views: 272
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
Reputation: 5664
Remove the second using statement for stream object which will fix the issue.
Upvotes: 0