Failed to delete image after opening it in vb.net application.

I have this code:

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

And I can’t delete the file, even if I uploaded it to the picture window. If I add this line:

 xx.dispose 

the image field turns large red X.

I only want to delete images when my application closes (these are temporary files). So will I just spare them before deleting them?

+6
image dispose
source share
1 answer

Do not use Image.FromFile , it keeps the file open.

From MSDN :

The file remains locked until the image is located.

Do this instead:

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

The file closes after the image is loaded, so you can delete the file if you need to

+12
source share

All Articles