if (textBoxUrl.Text.Trim() != "")//路径不为空
{
byte[] byteImage = null;//二进制变量
string[] files = Directory.GetFiles(this.textBoxUrl.Text);//根据路径获得数据保存在数组变量files中
foreach (string filepath in files)//遍历数据
{
pb.Image = Image.FromFile(filepath);//数据放在PictureBox pb里
//照片转成二进制数据,放在变量中
MemoryStream ms = new MemoryStream();
pb.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byteImage = new byte[ms.Length];
ms.Position = 0;
ms.Read(byteImage, 0, Convert.ToInt32(ms.Length));
//获得照片的名称
string iNum = Path.GetFileNameWithoutExtension(filepath);
if (GetPoster3(iNum))//调用查询方法判断,如果已经存在此名称的照片
{
UpdatePoster3(iNum, byteImage);//调用修改方法,修改对应的照片
}
else
{
InsertPoster3(iNum, byteImage);//调用插入方法插入一行数据
}
}
}上面这段代码总是报内存不足的错误,怎么也找不出原因,我要上传的照片大概有几十张吧,总是传不完就报错,帮忙想想办法哦!
{
byte[] byteImage = null;//二进制变量
string[] files = Directory.GetFiles(this.textBoxUrl.Text);//根据路径获得数据保存在数组变量files中
foreach (string filepath in files)//遍历数据
{
pb.Image = Image.FromFile(filepath);//数据放在PictureBox pb里
//照片转成二进制数据,放在变量中
MemoryStream ms = new MemoryStream();
pb.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byteImage = new byte[ms.Length];
ms.Position = 0;
ms.Read(byteImage, 0, Convert.ToInt32(ms.Length));
//获得照片的名称
string iNum = Path.GetFileNameWithoutExtension(filepath);
if (GetPoster3(iNum))//调用查询方法判断,如果已经存在此名称的照片
{
UpdatePoster3(iNum, byteImage);//调用修改方法,修改对应的照片
}
else
{
InsertPoster3(iNum, byteImage);//调用插入方法插入一行数据
}
}
}上面这段代码总是报内存不足的错误,怎么也找不出原因,我要上传的照片大概有几十张吧,总是传不完就报错,帮忙想想办法哦!
foreach (string filepath in files)/要传完一张再取下一张。并且执行 Dispose()
if (textBoxUrl.Text.Trim() != "")//路径不为空
{
byte[] byteImage = null;//二进制变量
string[] files = Directory.GetFiles(this.textBoxUrl.Text);//根据路径获得数据保存在数组变量files中
foreach (string filepath in files)//遍历数据
{
try
{
pb.Image = Image.FromFile(filepath);//数据放在PictureBox pb里
//照片转成二进制数据,放在变量中
MemoryStream ms = new MemoryStream();
pb.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byteImage = new byte[ms.Length];
ms.Position = 0;
ms.Read(byteImage, 0, Convert.ToInt32(ms.Length));
//获得照片的名称
string iNum = Path.GetFileNameWithoutExtension(filepath); if (GetPoster3(iNum))//调用查询方法判断,如果已经存在此名称的照片
{
UpdatePoster3(iNum, byteImage);//调用修改方法,修改对应的照片
}
Else
{
InsertPoster3(iNum, byteImage);//调用插入方法插入一行数据
} ms.Dispose();
ms=null;
byteImage=null; }
finally
{
pb.Image.Dispose();
pb.Image=null;
System.Windows.Forms.Application.DoEvents();
GC.Collect();
} }
}