Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem deleteting an image file

Posted on 2004-10-25
6
Medium Priority
?
262 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
Comment
Question by:Ghanisen
  • 3
  • 3
6 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 12403710
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
ID: 12404648
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 86

Expert Comment

by:Mike Tomlinson
ID: 12404704
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Ghanisen
ID: 12405166
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 86

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

916 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