0

我知道其他人也有类似的问题,但我的问题是特定于图像的......我有一个如下的图像功能:

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

1 回答 1

7

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

你应该:

  • 只调用Image.FromFile一次并重用对象Setimage(你在一个if条件下加载它两次......)
  • 用完后把所有Image东西都扔掉
  • BackgroundImage在设置新的之前处理旧的

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

于 2013-08-15T06:06:45.250 回答