错误:“进程无法访问....因为它正被另一个进程使用。”在“删除图像”中

时间:2013-08-15 06:01:55

标签: c# error-handling

我知道其他人有类似的问题,但我的问题是针对图像... 我有一个像下面这样的图像功能:

        static public string Setimage(PictureBox pictureBox, OpenFileDialog ofd,string nameform,string folderform)
    {
        ofd.Title = "Select Pictures";
        ofd.Filter = "Pictures(*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png | All file (*.*)| *.*";

        ofd.DefaultExt = ".jpg"; // Default file extension 
        string namefile = "";
        // Process open file dialog box results 

        if (ofd.ShowDialog() == DialogResult.OK)
        {
           // try
            //{
                string fileName = ofd.FileName;
                if (ofd.SafeFileName.Length <= 50)
                    if (Image.FromFile(fileName).Width >= 640 && Image.FromFile(fileName).Height >= 480)
                    {
                        namefile = ofd.SafeFileName;
                        if (namefile != "Null_0_Null" || namefile != null)
                        {
                         string oldPath = @ofd.FileName;
                         string newFileName = namefile;
                         newpath = Application.StartupPath + @"\userpictures\" + @"Apartment\";
                                    deladdress = newpath + folderform + @"\" + @newFileName;
                                    Random rand = new Random();
                                    string pp=newpath+folderform;
                                   // string pdest;

                                    #region Check Directory And File To copy
                                    if (Directory.Exists(newpath + folderform))
                                    {
                                        if (!File.Exists(newpath + folderform + @"\" + @newFileName))
                                            File.Copy(oldPath, newpath + folderform + @"\" + @newFileName);
                                       // else
                                       // {
                                          //  File.Delete(newpath + folderform + @"\" + @newFileName);
                                         //   File.Copy(oldPath, newpath + folderform + @"\" + @newFileName);
                                        //}
                                    }
                                    else
                                    {
                                        Directory.CreateDirectory(newpath + folderform);
                                        File.Copy(oldPath, newpath + folderform + @"\" + @newFileName);
                                    }
                                    #endregion
                                    pictureBox.BackgroundImage = Image.FromFile(newpath + folderform + @"\" + @newFileName);
                        }
                        else { MessageBox.Show("filename" + namefile + "Not valid"); }
                    }
                    else { MessageBox.Show("Size of file not valid"); }
                else { MessageBox.Show("size of name file not valid"); }
           // }
           // catch { MessageBox.Show("your file that you selected is not valid please select anyone."); }
        }
        return namefile;
    }

为了加载图像我有这个功能:

 static public void loadimage(PictureBox pictureBox, string img, string nameform, string folderform)
    {
        try
        {

            if (img != "Null_0_Null")
                if (!System.IO.File.Exists(Application.StartupPath + @"\userpictures\" + nameform + @"\" + folderform + @"\" + img))
                {
                    pictureBox.BackgroundImage = Image.FromFile(Application.StartupPath + "\\filepictures\\default4.PNG");
                }
                else
                {
                  pictureBox.BackgroundImage =Image.FromFile(Application.StartupPath + @"\userpictures\" + nameform + @"\" + folderform + @"\" + img);
                }
                }
        catch { }
    }

在我的表格中,我称之为此功能。对于set image,我在表单中写了一个私有字符串:

string img1;

要在我的表单中加载图片,请加载:

loadimage(pictureBox1, "Blue hills.jpg","me", "Apartment");
img1 = "Blue hills.jpg";    

对于Setimage我有这个:

img1=Setimage(pictureBox1, openFileDialog1,"me", "Apartment");

当我使用此代码删除图片时显示错误“进程无法访问...”

 System.IO.File.Delete("image path");

1 个答案:

答案 0 :(得分:7)

当您使用Image.FromFile时,将打开该文件的文件句柄并保持打开,直到图像被丢弃。

你应该:

  • 只需拨打Image.FromFile一次,然后重复使用Setimage中的对象(您在单if条件下加载两次...)
  • 完成后每Image处理
  • 在设置新BackgroundImage之前处置旧Image

只要在删除文件之前丢弃与文件相关的每个{{1}},就应该没问题。