![](/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.