【问题标题】:Error: "The process cannot access.... because it is being used by another process. " in Delete Image错误:“该进程无法访问......因为它正被另一个进程使用。”在删除图像
【发布时间】:2016-12-07 09:13:14
【问题描述】:

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

        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 { }
    }

在我的表单中,我称之为函数。对于设置图像,我在表单中写了一个私有字符串:

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");

【问题讨论】:

  • 我确实希望 catch { } 不在您的 真实 代码中...(并且缩进更易读,理想情况下还有更多约定方法名称...... .)
  • Image 是一次性的(并持有原始图像文件的锁),但是您使用那些甚至不分配给局部变量的 FromFile 方法到处泄漏它们.
  • 您没有在某处关闭文件句柄。使用using 块,您就不必再担心这个了。

标签: c# error-handling


【解决方案1】:

当您使用Image.FromFile 时,它将打开该文件的文件句柄并保持打开状态直到图像被释放。

你应该:

  • 只调用一次Image.FromFile 并重用Setimage 中的对象(您在一个if 条件下加载了两次...)
  • 用完后将每个 Image 丢弃
  • 在设置新的BackgroundImage 之前,请处理掉旧的@

只要在删除文件之前处理掉所有与该文件相关的Image,就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多