Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 749
Jonathan. Avatar asked Jan 16 '10 23:01

Jonathan.


1 Answers

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

like image 118
Thomas Levesque Avatar answered Sep 20 '22 08:09

Thomas Levesque