2011-05-26 44 views
0

我必須在由相機捕獲的圖像上畫一些東西。這適用於許多設備,但有時RAM太小或圖片太大,並且該功能會因OutOfMemory異常而崩潰。新位圖上的OutofMemory異常()

我怎麼能: 一)優化代碼,以防止這種異常 二)處理這個異常(從而使照片更小,可用RAM等

這裏是代碼:

Dim from_bmp As Bitmap 
Dim bmp As Bitmap 

from_bmp = New Bitmap(picname) 
'I also tryed this with a function which is creating the Bitmap from Filestream 
'I also tryed this with the OpenNETCF.Drawing.Imaging.IImage 
'If the Original Pictiure is too big, the function will crash here. 

bmp = New Bitmap(from_bmp.Width, from_bmp.Height + stampheight) 
'now I Create a bitmap which is higher than the original, in order to write the stamp beneth the picture 

Dim font As New System.Drawing.Font("Arial", 30, FontStyle.Regular) 
gr = Graphics.FromImage(bmp) 
gr.DrawImage(from_bmp, 0, 0) 
from_bmp.Dispose() 
'here I draw somethin in the Bitmap 
bmp.Save(deststring, System.Drawing.Imaging.ImageFormat.Jpeg) 
gr.Dispose() 
bmp.Dispose() 
+0

升級系統內存是最好的選擇 – Predator 2011-05-26 15:08:39

+0

@Gens這種異常通常出現在很多平臺上,不僅僅是Windows,而不僅僅是.NET。 – 2011-05-26 15:13:53

+0

這是net.cf,代碼在移動設備上運行,我無法升級它們的Memorya^^ – 2red13 2011-05-26 15:14:21

回答

2

我可能會在您的位圖上使用「使用」模式,並且請注意,通過簡單再次嘗試就可以解決創建位圖時的OOM問題(here's a diatribe至於爲什麼)。我敢打賭,GC堆太滿了並且第二次請求,特別是在收集之後,將成功。

我會仔細檢查剩下的代碼(未顯示),並確保所有其他圖形對象都正確放置 - CF不會自動清理圖形對象的本機資源並且經常需要一些幫助(再次參見上面的鏈接)。

+0

是的,我應該提到,我執行一個GC.collect(),我處理每個Bitmaps我使用和在catch塊我GC.WaitForPendingFinalizers後再次嘗試 – 2red13 2011-05-26 15:17:55