我从数据库中导出大量数据(特别是图像)我试图处理这些对象,但似乎内存分配不断堆积。请在这里帮助我的代码
foreach (var v in EntityFrameworkContext.GetInstance.DbContext.ATasks.ToList())
{
if (v.TaskImages.Count == 0)
continue;
var dir = string.Format("{0}\\{1}", txtDumpFolder.Text, v.Id);
if (!Directory.Exists(dir))
Directory.CreateDirectory(string.Format("{0}\\{1}", txtDumpFolder.Text, v.Id));
lstLog.Invoke((MethodInvoker)delegate()
{
lstLog.Items.Add("---------------------------------------------------------------------");
lstLog.Items.Add(string.Format("DIRECTORY: {0} Created.", dir));
int visibleItems = lstLog.ClientSize.Height / lstLog.ItemHeight;
lstLog.TopIndex = Math.Max(lstLog.Items.Count - visibleItems + 1, 0);
});
v.TaskImages.ToList().ForEach(a =>
{
using (Bitmap bmp = new Bitmap(new MemoryStream(a.Image)))
{
PropertyItem[] pi = bmp.PropertyItems;
bmp.Save(string.Format("{0}\\{1}.jpg", dir, a.Id), ImageFormat.Jpeg);
LogMessage(string.Format("IMAGE: {0} \n exported.", a.Id));
}
});
GC.Collect();
};
}