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.
GhanisenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
You can use code like this to prevent a file lock on the image:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        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
    End Sub

~IM
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GhanisenAuthor Commented:
Hi Idle Mind,

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.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

GhanisenAuthor Commented:
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
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
GhanisenAuthor Commented:
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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.