【发布时间】:2010-12-07 20:08:41
【问题描述】:
我正在尝试将一些图片复制到 RAM,但这会导致内存不足异常.. 我不知道为什么,但我认为这是“冻结()”的原因。但是如何“解冻”,这真的是问题吗?
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 块中不是答案。
-
我知道.. 这是尝试和错误,因为我不知道如何修复此泄漏
-
您尝试处理的图像有多大?
-
1378x2000.. 由于某些缩放功能,我无法缩小它们。
标签: c# visual-studio multithreading