ma初学者设计一个在硬盘上保存图像的测试应用程序及其在sql表中的名称.我能够保存,浏览记录但无法删除图像.
它给了我错误进程无法访问文件'x',因为删除图像时其他进程正在使用它
代码如下:
Private Sub btnDelete_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnDelete.Click isProcName ="btnDelete_Click"OBJ = New clsImageStoring
Try
Dim result As DialogResult = MessageBox.Show(Me, "Do you really want to delete this Record?", "Query", vbYesNo, vbQuestion)
If result = Windows.Forms.DialogResult.Yes Then
iiId = DGV.Rows(iiRowno).Cells(0).Value
iiImageNo = DGV.Rows(iiRowno).Cells(1).Value
Dim liTempImageNo As Int64 = 0
If OBJ.Delete(iiId) Then
Fillgrid()
liTempImageNo = DGV.Rows(0).Cells(1).Value
picEmp.Image.Dispose()
picEmp.Image = Image.FromFile("D:\EmpImages\" & liTempImageNo & ".jpg")
'File.Delete("D:\EmpImages\" & iiImageNo & ".jpg")
FileIO.FileSystem.DeleteFile("D:\EmpImages\" & iiImageNo & ".jpg")
MessageBox.Show(Me, "Record Deleted Successfully", "Information", vbOKOnly, vbInformation)
End If
End If
Catch ex As Exception
clsLog.WriteException(ex, isModuleName, isProcName)
End Try
End Sub
Run Code Online (Sandbox Code Playgroud)
我尝试处理图像和图片框并在图片框中加载另一个图像也不使用任何文件对象打开文件,除了Image类的From文件方法.
任何帮助将不胜感激谢谢
处置不起作用.它没有处理图像仍然使用的图像,但当我尝试手动删除该特定图像时,我向我显示错误,该文件已被"vshost.exe"使用,这是我的应用程序本身.所以我使用文件流来加载图片框中的图片,正如James所说,但当我尝试使用以下代码删除任何图像时,它仍然给我错误:
File.Delete("D:\ EmpImages \"&iiImageNo&".jpg")
File.Delete("D:\ EmpImages \"&iiImageNo&".jpg")
所以我试过这个
FileSystem.Kill("D:\ EmpImages \"&iiImageNo&".jpeg")
它起作用了.谢谢詹姆斯和所有为我提供宝贵时间的人
图片框可能仍然没有被你试图从磁盘上删除它的时候发布的图像,更可靠的方法是从加载图像Stream
如
Using fs As New System.IO.FileStream("file path", IO.FileMode.Open, IO.FileAccess.Read)
PictureBox1.Image = System.Drawing.Image.FromStream(fs)
End Using
Run Code Online (Sandbox Code Playgroud)
这样可以防止对文件进行任何类型的锁定.
它似乎是一个常见的问题.