用C#处理大图像,内存占用十分惊人,该如果解决?
程序中用一个循环对某文件夹下所有图像文件做处理,图像本身比较大(2848*4256),图像文件本身占用内存很大,而且在.NET中分配的内存不能像C++那样手动立即释放,GC的垃圾收集所释放的内存,抵不上程序处理所占用的内存.导致很快就会出现内存不够用的现象,实在想不出什么解决方案,不知大家有没有好的方法..谢谢!!!!!!
for(int i=0; i<= n; i++)
{
TransSingleImage(...);
}
private void TransSingleImage(string str)
{
Bitmap bit = null;
Bitmap bitnew = null;
Graphics g = null;
Image img = null;
string filename;
try
{
img = Image.FromFile(str);
filename = Path.GetFileName(str);
}
catch
{
return;
}
...
bitnew.Save(Path.Combine(dc,file) , f);
g.Dispose();
bit = null;
bitnew = null;
img = null;
catch(Exception ex)
{
MessageBox.Show("保存文件时出错");
}
}