Solved

Problem deleteting an image file

Posted on 2004-10-25
232 Views
Last Modified: 2010-04-24
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.
0
Question by:Ghanisen
    6 Comments
     
    LVL 85

    Accepted Solution

    by:
    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
     

    Author Comment

    by:Ghanisen
    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
     
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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
     

    Author Comment

    by:Ghanisen
    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
     
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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
     

    Author Comment

    by:Ghanisen
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Anonabox PRO Tor & VPN Router

    PRO is the most advanced way to fortify your privacy and online anonymity by layering the Tor network with VPN services. Use both together or separately, and without needing to download software onto your devices.

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    860 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now