![](/img/trans.png)
[英]OutOfMemory Exception in <BitmapImage>.SetSource()
[英]Opening large Images using BitmapImage.SetSource - Insufficient Memory Exception
我正在创建一个Silverlight 4应用程序,它需要在将图像上传到服务器之前显示图像的缩略图。 我的代码完美适用于15mb以下的图像但是当我尝试打开大图像(一些超过30mb)时,我得到以下例外:
Insufficient memory to continue the execution of the program.
错误是非常自我解释但是我的问题是....是否有另一种方法可以打开大图像或增加Silverlight应用程序可用的内存?
我在具有8GB RAM的机器上进行测试,当我检查IE进程时,托管应用程序内存使用率达到250mb左右,然后抛出异常,因此可以相当安全地假设我的机器内存不足。
我用来打开完整图像的代码如下,虽然我已经省略了用于生成调整大小的缩略图的代码,因为它目前从未在大图像中得到这么多:
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();
}
}
你基本上要么内存不足(记得Silverlight是沙盒)和/或资源(即Handles或类似的)。
检查一下,找出您描述的问题的解决方案,包括源代码等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.