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

二次信任 提交于 2019-12-29 07:01:08

问题


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.

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?


回答1:


Don't use Image.FromFile, it keeps the file open.

From 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



来源:https://stackoverflow.com/questions/2079428/unable-to-delete-image-after-opening-it-in-vb-net-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!