Ghanisen
asked on
Problem deleteting an image file
Hi,
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
End If
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.
I tried:
picPhoto.Image = Nothing
picPhoto.Image.Dispose()
before :
Kill(PhotoPath)
Or
File.Delete(PhotoPath)
None works.
Any help shall be greatly appreciated.
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
End If
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.
I tried:
picPhoto.Image = Nothing
picPhoto.Image.Dispose()
before :
Kill(PhotoPath)
Or
File.Delete(PhotoPath)
None works.
Any help shall be greatly appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Something else must have a lock on the file. I just tried this and it deleted the file immediately without any errors.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim PhotoPath As String = "C:\Documents and Settings\Michael\My Documents\My Pictures\Copy of dev5_1024.jpg"
If File.Exists(PhotoPath) = True Then
Dim tempImg As Image = Image.FromFile(PhotoPath)
picPhoto.Image = New Bitmap(tempImg)
tempImg.Dispose()
Else
picPhoto.Image = Nothing
End If
Kill(PhotoPath)
End Sub
What else are you doing with regards to the file in your app?
~IM
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim PhotoPath As String = "C:\Documents and Settings\Michael\My Documents\My Pictures\Copy of dev5_1024.jpg"
If File.Exists(PhotoPath) = True Then
Dim tempImg As Image = Image.FromFile(PhotoPath)
picPhoto.Image = New Bitmap(tempImg)
tempImg.Dispose()
Else
picPhoto.Image = Nothing
End If
Kill(PhotoPath)
End Sub
What else are you doing with regards to the file in your app?
~IM
ASKER
Hi Idle Mind,
I'm doing nothing else than loading the file with:
If File.Exists(PhotoPath) = True Then
picPhoto.Image = Image.FromFile(PhotoPath)
Else : picPhoto.Image = Nothing
End If
I don't understand why it works for you and not for me?
Is the image actually in the pictureBox when you're deleting it? because that's the case for me.
Thanks
I'm doing nothing else than loading the file with:
If File.Exists(PhotoPath) = True Then
picPhoto.Image = Image.FromFile(PhotoPath)
Else : picPhoto.Image = Nothing
End If
I don't understand why it works for you and not for me?
Is the image actually in the pictureBox when you're deleting it? because that's the case for me.
Thanks
Yes, the image is in the PictureBox when I delete it.
Try creating a new project with just a PictureBox and a Button and the code I gave you and see if it works.
~IM
Try creating a new project with just a PictureBox and a Button and the code I gave you and see if it works.
~IM
ASKER
Hi Idle Mind,
I found out the problem. Nothing to do with your code which works fine.
I was storing the original Photopath in a Private variable to keep it for deletion time. Once I changed that by storing the information in a hidden textbox evrything went OK.
Anyway thanks a lot Idle Mind and sorry for the hussle. You sure deserved the 500 points!
I found out the problem. Nothing to do with your code which works fine.
I was storing the original Photopath in a Private variable to keep it for deletion time. Once I changed that by storing the information in a hidden textbox evrything went OK.
Anyway thanks a lot Idle Mind and sorry for the hussle. You sure deserved the 500 points!
ASKER
I've just tried:
If File.Exists(PhotoPath) = True Then
Dim tempImg As Image = Image.FromFile(PhotoPath)
picPhoto.Image = New Bitmap(tempImg)
tempImg.Dispose()
Else
picPhoto.Image = Nothing
End If
Followed by :
Kill((PhotoPath)
or
File.Delete((PhotoPath)
Unfortunately it doesn't work in both cases. The file still can't be deleted.
Still need your help. Thanks.