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);//调用插入方法插入一行数据
         }
      }
}上面这段代码总是报内存不足的错误,怎么也找不出原因,我要上传的照片大概有几十张吧,总是传不完就报错,帮忙想想办法哦!

解决方案 »

  1.   

    你上传完照片是不是没有释放资源啊,在最后调用下ms.close()试试
      

  2.   

    多用using释放相关资源,看看资源消耗情况
      

  3.   

    不要进行
    foreach (string filepath in files)/要传完一张再取下一张。并且执行 Dispose()
      

  4.   


    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();
         }  }
    }
      

  5.   

    用递归的方式实现,每次传完一张再调用自身进行第二章传递,起码也得开一个线程吧(如果是Winform)。要不主线程不就阻塞了