如何从特定单元格中删除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]`
}
}
答案 0 :(得分:2)
我遇到了同样的问题,看到了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;
}