一个ListBox,
 private void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                dabuID = Convert.ToInt32(listBox1.SelectedValue.ToString());
                tbPart.Text = listBox1.Text;
                picbxPort.Image = Image.FromFile(Application.StartupPath + md.Get_A_Dabu(listBox1.SelectedValue.ToString()).PicUrl);
                tbPicUrl.Text = "";
            }
            catch { }
        }
删除选中的某个项时,顺便删除它的图片:
private void tsbtnDel_Click(object sender, EventArgs e)
        {
            if (dabuID == 0)
            {
                MessageBox.Show("请选择要删除的大部!");
            }
            else
            {
                if (MessageBox.Show("确定要删除?", "此删除不可恢复!", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    try
                    {
                        string oldPath = Application.StartupPath + md.Get_A_Dabu(dabuID.ToString()).PicUrl;
                        picbxPort.Image = Image.FromFile(Application.StartupPath + "\\DabuImage\\default.gif");
                        Image.FromFile(oldPath).Dispose();
                        md.Del(dabuID.ToString());
                        BindLb(cbbAssType.SelectedValue.ToString());
                        File.Delete(oldPath);
                        
                       
                        MessageBox.Show("删除成功!");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
错误就在 File.Delete(oldPath);
提示另外一个进程正在使用oldPath所指示的图片!哪里的错误呢?该怎么办?
picbxPort.Image = Image.FromFile(Application.StartupPath + "\\DabuImage\\default.gif");
                        Image.FromFile(oldPath).Dispose();
这两句去掉也是这个错误!

解决方案 »

  1.   

    listBox1_SelectedValueChanged
    里面也用到了Image.FromFile,是不是同一个文件啊而且
    Image.FromFile(oldPath).Dispose();
    这种写法是什么意思啊,画蛇添足?
      

  2.   

    读取图片的时候尝试用流的方式读取试试
    ------------------
    Image.FromFile这种方法一定不行???
      

  3.   

    是引用到了,Image.FromFile(oldPath).Dispose();
    我本以为是为了释放已经引用的部分
      

  4.   

    不知道楼主的oldPath 和Application.StartupPath + "\\DabuImage\\default.gif"
    这两个图片有什么关系,
    肯定是什么地方的Image没有dispose掉,
    建议楼主Image SourceImage = Bitmap.FromFile(路径);
    然后SourceImage.dispose();
      

  5.   

    Image.FromFile(oldPath).Dispose();不会立即释放,只是告诉他可以释放,什么时候释放就不知道了
    肯定是图片资源还在内存中,没有释放
    picbxPort.Image=null;
    不知道这样可以么。。
      

  6.   

    各位遇到这种读取图片的情况是怎么做的啊?
    picbxPort.Image = Image.FromFile(Application.StartupPath + md.Get_A_Dabu(listBox1.SelectedValue.ToString()).PicUrl);
    这种方式不行吗?
      

  7.   

     picbxPort.Image = Image.FromFile(Application.StartupPath + md.Get_A_Dabu(listBox1.SelectedValue.ToString()).PicUrl);
                    tbPicUrl.Text = "";这来也的需要dispose
      

  8.   

      string oldPath = Application.StartupPath + md.Get_A_Dabu(dabuID.ToString()).PicUrl;
                            picbxPort.Image = Image.FromFile(Application.StartupPath + "\\DabuImage\\default.gif");
                            Image.FromFile(oldPath).Dispose();
                            md.Del(dabuID.ToString());
                            BindLb(cbbAssType.SelectedValue.ToString());
    picbxPort.Dispose();//不知道你的方法能不能点出Dispose();来
                            File.Delete(oldPath);
      

  9.   

    如楼上有位所说,用流文件操作,有OPEN和CLOSE.
      

  10.   

    //加载
    private void Form2_Load(object sender, EventArgs e)
            {
                this.pictureBox1.Image = Image.FromFile("Blue hills.jpg");
            }//删除
      private void button1_Click(object sender, EventArgs e)
            {
                this.pictureBox1.Image.Dispose();
                this.pictureBox1.Image = null;
                System.IO.File.Delete("Blue hills.jpg");
            }