【问题标题】:How can I remove image from datagridview image column from specific cell, actually I have to remove image from directory from specific folder如何从特定单元格的 datagridview 图像列中删除图像,实际上我必须从特定文件夹的目录中删除图像
【发布时间】:2013-11-29 06:22:12
【问题描述】:

如何从特定单元格的 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]`
   }
}

【问题讨论】:

    标签: c#-4.0


    【解决方案1】:

    我遇到了同样的问题,并在 Code Project 上看到了一个帖子: 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;
               }
    

    【讨论】:

    • 请在答案中包含解决方案的相关部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 2019-09-05
    • 1970-01-01
    相关资源
    最近更新 更多