我想將一些圖片複製到RAM,但這會導致內存不足例外。 我不知道爲什麼,但我認爲這是「freeze() 」。但如何「解凍」,這真的是問題嗎?內存不足異常C#freezable.freeze
public void preLoadThread(Object o)
{
Overlay ov = (Overlay)o;
ImageSource tempNext = BitmapConverter(ov.tempPreLoadPathNext);
ImageSource tempPrev = BitmapConverter(ov.tempPreLoadPathPrev);
tempNext.Freeze();
tempPrev.Freeze();
ov.Dispatcher.Invoke(
DispatcherPriority.Normal,
(Action)delegate()
{
ov.preLoadedNext = tempNext;
ov.preLoadedPrev = tempPrev;
ov.preLoadPathNext = ov.tempPreLoadPathNext;
ov.preLoadPathPrev = ov.tempPreLoadPathPrev;
}
);
}
public BitmapSource BitmapConverter(String path)
{
System.Drawing.Bitmap b = null;
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite))
{
try
{
b = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromStream(fs);
}
catch (Exception)
{
GC.Collect();
GC.WaitForFullGCComplete();
}
fs.Close();
}
if (b == null)
{
// Error
return null;
}
BitmapSizeOptions options = BitmapSizeOptions.FromEmptyOptions();
BitmapSource bs = null;
try
{
bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
b.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
options);
}
catch (Exception)
{
GC.Collect();
GC.WaitForFullGCComplete();
}
return bs;
}
GC.Collect在catch塊中不能作爲答案。 – 2010-12-07 20:19:58
我知道..這是嘗試和錯誤,因爲我不知道如何解決這個泄漏 – Martin 2010-12-07 20:25:35