Problem deleteting an image file
Posted on 2004-10-25
I have a PictureBox (picPhoto) where I load an image from an image file.
The file’s path and name is in the string PhotoPath.
This is the code I use for loading the image from the file into the pictureBox:
If File.Exists(PhotoPath) = True Then
picPhoto.Image = Image.FromFile(PhotoPath)
Else : picPhoto.Image = Nothing
The user may do the following:
1. change the file name while keeping the same path = NewPhotoPath. In this case we have to be able to delete the initial file (PhotoPath) which becomes useless ;
2. load another image into the pictureBox through an OpenFileDialog and want to save it under the original file name and path (PhotoPath). In this case again we have to be able to delete or overwrite the initial file (PhotoPath) because it contains the old image;
3. do both. In this case again we have to be able to delete the initial file (PhotoPath) because it contains the old image and it has become useless;
In all cases we have to delete the original image file (PhotoPath) and that’s my problem because it seems that somehow the file remains in use and therefore cannot be deleted.
picPhoto.Image = Nothing
Any help shall be greatly appreciated.