简体   繁体   English

在vb.net应用程序中打开后无法删除图像

[英]unable to delete image after opening it in vb.net app

I have this code: 我有这个代码:

Dim xx as image
xx = image.fromfile(Fileloc)
picturebox.image = xx

And i can't delete the file even though I've loaded it into a picture box. 即使我已将其加载到图片框中,我也无法删除该文件。 If I add this line: 如果我添加这一行:

xx.dispose

the picture box becomes a big red X. 图片框变成了一个大红色的X.

I only want to delete the images when my application is closing (they are temp files). 我只想在应用程序关闭时删除图像(它们是临时文件)。 So shall I just dispose them before I delete them? 所以,在删除它们之前,我应该将它们丢弃吗?

Don't use Image.FromFile , it keeps the file open. 不要使用Image.FromFile ,它会保持文件打开。

From MSDN : 来自MSDN

The file remains locked until the Image is disposed. 文件保持锁定状态,直到图像被丢弃。

Do that instead : 这样做:

Dim xx as Image
Using str As Stream = File.OpenRead(Fileloc)
    xx = Image.FromStream(str)
End Using
picturebox.Image = xx

The file is closed after the image is loaded, so you can delete the file if you need to 加载图像后文件将关闭,因此您可以根据需要删除该文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM