c#-4.0 - 如何从特定单元格的 datagridview 图像列中删除图像,实际上我必须从特定文件夹的目录中删除图像

标签 c#-4.0

如何从特定单元格的 datagridview 图像列中删除图像,实际上我必须从特定文件夹的目录中删除图像,但它给出了图像被另一个进程使用的错误,所以请帮助我

filefullPath = Application.StartupPath + "\\PatientDocuments\\" + patId + "\\" + imge;

if (e.ColumnIndex == 5)
{
   DialogResult mesg = MessageBox.Show("Are You Sure Do You Want To Delete?", "Delete", MessageBoxButtons.YesNo);

   if (mesg == DialogResult.Yes)
   {
       BL_db.ImgId = imgeid;
       int i = Convert.ToInt32(dgvImage.SelectedRows[0].Index.ToString());

       dgvImage.Rows.RemoveAt(i);
       BL.Delete_PatientImage(BL_db);
       //DataGridViewImageColumn imgcolumn = new DataGridViewImageColumn();
       //imgcolumn.Dispose();

       //DataGridViewImageCell imgcell = new DataGridViewImageCell();
       // imgcell = (DataGridViewImageCell)dgvImage.Rows[dgvImage.CurrentRow.Index].Cells[2];
       //imgcell.Dispose();

       //---------for delete file from directory-----------------------
       string[] arr1 = Directory.GetFiles(Application.StartupPath + "\\PatientDocuments\\" + patId, "*.jpg");

       foreach (string filePath in arr1)
       {
          if (filePath.Contains(".jpg"))
             File.Delete(filePath);
       }

       //-------------------------------------------------![enter image description here][1]`
   }
}

最佳答案

我遇到了同样的问题,并在代码项目上看到了一个帖子: See Solution 2

它解决了我的问题。

编辑:添加了链接中的重要部分...

基本上,Image.Load 方法会导致文件在程序运行期间被锁定。很难处置它。

以这种方式加载图像不会导致锁定问题。

// creating a bitmap from a stream
           using (FileStream fileStream = File.Open(Filename, FileMode.Open))
           {
               Bitmap bitmap = new Bitmap(fileStream);
               Image currentPicture = (Image)bitmap;
           }

关于c#-4.0 - 如何从特定单元格的 datagridview 图像列中删除图像,实际上我必须从特定文件夹的目录中删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20279593/

相关文章:

c# - 以前从未使用过 LocalDB

asp.net - 如何使用带有返回类型的 System.Action?

c# - 在单行语句中删除包含某个单词/字符串的数组中的元素

c# - 是否需要注销事件处理程序?

c# - 如何检测图像中的黑色子弹?

.net - 使用 X509Certificate2 和 ECC 公钥加载证书

c# - 为什么这个自定义字典类不起作用 - C# 4.0

c# - 将 byte[] 附加到 MemoryStream

node.js - MongoDB : Update only those objects in a collection whose 'Id' exists in a list

sql-server - 使用单例类创建数据库上下文是一种好习惯吗?