该进程无法访问该文件,因为该文件正在被另一个进程使用
我通过以下代码将图像添加到 FlowLayoutPanel 控件中。
Dim WithEvents Pedit As DevExpress.XtraEditors.PictureEdit
Private Sub LoadImagesCommon(ByVal fi As FileInfo)
Pedit = New DevExpress.XtraEditors.PictureEdit
Pedit.Width = 133
Pedit.Height = 98
Pedit.Image = Image.FromFile(fi.FullName)
Pedit.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom
Pedit.ToolTip = fi.Name
AddHandler Pedit.MouseClick, AddressOf Pedit_MouseClick
AddHandler Pedit.MouseEnter, AddressOf Pedit_MouseEnter
AddHandler Pedit.MouseLeave, AddressOf Pedit_MouseLeave
FlowLayoutPanel1.Controls.Add(Pedit)
End Sub
问题是,当我尝试删除我在上一步中加载的图像。
FlowLayoutPanel1.Controls.Clear()
FlowLayoutPanel1.Refresh()
For Each fi As FileInfo In New DirectoryInfo(My.Settings.TempDirectory).GetFiles
RemoveHandler Pedit.MouseClick, AddressOf Pedit_MouseClick
RemoveHandler Pedit.MouseEnter, AddressOf Pedit_MouseEnter
RemoveHandler Pedit.MouseLeave, AddressOf Pedit_MouseLeave
File.Delete(fi.FullName)
Next
那么我在这里做错了什么?
I am adding images to a FlowLayoutPanel control via the following code
Dim WithEvents Pedit As DevExpress.XtraEditors.PictureEdit
Private Sub LoadImagesCommon(ByVal fi As FileInfo)
Pedit = New DevExpress.XtraEditors.PictureEdit
Pedit.Width = 133
Pedit.Height = 98
Pedit.Image = Image.FromFile(fi.FullName)
Pedit.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom
Pedit.ToolTip = fi.Name
AddHandler Pedit.MouseClick, AddressOf Pedit_MouseClick
AddHandler Pedit.MouseEnter, AddressOf Pedit_MouseEnter
AddHandler Pedit.MouseLeave, AddressOf Pedit_MouseLeave
FlowLayoutPanel1.Controls.Add(Pedit)
End Sub
The problem is that i get the following error The process cannot access the file xxxx because it is being used by another process.
when i try to delete the images that i loaded on the previous step.
FlowLayoutPanel1.Controls.Clear()
FlowLayoutPanel1.Refresh()
For Each fi As FileInfo In New DirectoryInfo(My.Settings.TempDirectory).GetFiles
RemoveHandler Pedit.MouseClick, AddressOf Pedit_MouseClick
RemoveHandler Pedit.MouseEnter, AddressOf Pedit_MouseEnter
RemoveHandler Pedit.MouseLeave, AddressOf Pedit_MouseLeave
File.Delete(fi.FullName)
Next
So what am i doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Image.FromFile
实际上锁定了它加载的文件 并且只有在释放锁后才释放锁。解决方案是将图像绘制到另一个图像的图形上下文中(从而有效地复制它)并处理原始图像。
Image.FromFile
actually locks the file it loads and only releases the lock once it is disposed.The solution is to draw the image into another image’s graphics context (thus effectively copying it) and dispose the original image.
啊哈!谢谢康拉德。
经过一番阅读后,我也找到了另一种解决方法。
更新:
这是康拉德的建议。对于所有新手来说,就像我一样:)
这更好,因为在 FileStream 关闭后 Image 对象无法调用其 Save 方法。
Aha! Thank you Konrad.
after some reading, i found another workaround too.
Update:
and here is what Konrad suggested. For all the newbies out there, just like me :)
which is better since the Image object cannot have its Save method called after the FileStream has been closed.
我发现这个解决方案是在图像文件加载到 PictureBox 后解锁图像文件的最佳方法:
PictureBoxName.LOAD(带有完整路径的图像文件名)
I found this solution is the best to unlock the image file after it has been loaded to the PictureBox :
PictureBoxName.LOAD(image file name with full path)