我知道其他人有类似的问题,但我的问题是针对图像...我有一个像下面的图像功能:
        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");
使用Image.FromFile时,将打开该文件的文件句柄并保持打开状态,直到处理完图像.
你应该:
Image.FromFile一次并重新使用该对象Setimage(你在一个if条件下加载它两次......)Image当你完成它时,每一次处理BackgroundImage在设置新旧之前处置旧的只要Image在删除文件之前丢弃与文件相关的每个文件,就应该没问题.