I need to dynamically load many (sometimes hundreds) thumbnails. For performance reasons, I need to do this in a limited number of requests, I use a single request / response for testing. I send binary data for images in response and upload it to BitmapImage using a MemoryStream. This works correctly until I load more than 80 thumbnails, and then get the Catastrophic Error exception. To make sure my data is not corrupted, I tried several times to load BitmapImage with the same byte array, and it will work after loading about 80.
The following is an example of how an image is loaded from an array of bytes. The byte array is known to have the correct image data (png):
private BitmapImage LoadImage(byte[] imageData) { BitmapImage img = new BitmapImage(); MemoryStream stream = new MemoryStream(imageData); img.SetSource(stream);
Then I use BitmapImage as the source for the image element on the page, but the error occurs in the line img.SetSource(...) above.
Adding GC.Collect() to the loop where I upload the thumbnails allows a few more images to be loaded, so I think this has something to do with memory management, but I don't know what I can do to fix the problem.
toby
source share