Reputation: 4892
I am creating a Silverlight 4 application that needs to display thumbnails of images before uploading them to the server. The code I have works perfectly for images under 15mb but when I try and open large images (some over 30mb) I get the following Exception:
Insufficient memory to continue the execution of the program.
The error is pretty self explanatory however my question is....is there an alternative way of opening large images or increasing the memory available to the Silverlight application?
I am testing this on a machine with 8gb of RAM and when I check the IE process hosting the application memory usage peaks at around 250mb before throwing the exception so its fairly safe to assume my machine is not running out of memory.
The code I am using to open the full image is as follows although I have omitted the code for generating the resized thumbnails as it currently never gets that far with large images:
private BitmapImage OpenImage(Stream stream)
{
byte[] fullRead = this.ReadFully(stream);
MemoryStream ms = new MemoryStream(fullRead);
BitmapImage bi = new BitmapImage();
bi.SetSource(ms);
return bi;
}
private byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[input.Length];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
Upvotes: 0
Views: 2882