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