从Sql Server中Image字段导出保存JPG格式 内存溢出的问题

whmjw 2008-04-01 10:26:12
现在有一个客户,我们要换他们的软件,他们有会员,大概总共有20000多个,绝大部分都 有会员的图片,可原来的老程序放进去的好像是BMP格式的,占用的空间太大,他们的库容量是6G,图片占了绝大部分大概有5G,我现在要做的就是把他们数据库里图片导出来,并保存为JPG格式,不过我写的东西老是报内存溢出,请大家看一下代码有什么问题,谢谢
DataTable dt=Global.Query("select max(id)  from t_discount_card where photo is not null");
int totalcount=int.Parse(dt.Rows[0][0].ToString());//保存大的图片ID
int id=0;
string sql="";
for(;id<totalcount;)
{
if(id==0)
sql="select top 20 id,photo from t_discount_card where photo is not null order by id";
else
sql="select top 20 id,photo from t_discount_card where photo is not null and id>"+id.ToString()+"order by id";
if(totalcount==id) //如果是最后一个图片,则退出
return;
DataTable dt1=Global.Query(sql);
for(int count=0;count<dt1.Rows.Count;count++)
if(dt1.Rows[count]["photo"]!=System.DBNull.Value)
{
byte[] imgdata=((byte[])(dt1.Rows[count]["photo"]));
if(imgdata!=null && imgdata.Length>100)
{
MemoryStream myStream=new MemoryStream();
foreach(byte a in imgdata)
{
myStream.WriteByte(a);
}
if(File.Exists(Application.StartupPath+@"\pic\"+dt1.Rows[count]["id"].ToString()+".jpg"))
File.Delete(Application.StartupPath+@"\pic\"+dt1.Rows[count]["id"].ToString()+".jpg");
Image.FromStream(myStream).Save(Application.StartupPath+@"\pic\"+dt1.Rows[count]["id"].ToString()+".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
myStream.Close();
myStream=null;
}
id=int.Parse(dt1.Rows[count]["id"].ToString());
}
dt1.Dispose();
GC.Collect();
}

我的思路是每20个图片的读并写入成文件,保存为JPEG格式,现在的问题是大概导出600多张图片后,就报内存溢出了
...全文
212 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dancingbit 2008-04-01
  • 打赏
  • 举报
回复
Image怎么会没有Dispose方法?
我要是你,保存那个地方就会这么写:


Image img=Image.FromStream(myStream);
img.Save(Application.StartupPath+@"\pic\"+dt1.Rows[count]["id"].ToString()+".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
whmjw 2008-04-01
  • 打赏
  • 举报
回复
请问如何Dipose
Image没有Dipose这个方法呀?
yuanjun_xf 2008-04-01
  • 打赏
  • 举报
回复
Image在Save之后没有Dispose
楼上的正解
dancingbit 2008-04-01
  • 打赏
  • 举报
回复
Image在Save之后没有Dispose。
whmjw 2008-04-01
  • 打赏
  • 举报
回复
自己解决了:)

111,111

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧