Delete Picture File

LebenOjanen

New member
Joined
May 11, 2005
Messages
4
Programming Experience
5-10
I am making a program that allows users to enter information into a database and import pictures from other areas of their computer. The selected picture is copied to an Images directory inside the program directory.

However I cannot delete the image when the delete function is called. The error message I get says the image is still in use. I have tried the picturebox.Nothing, etc., commands and still recieve this error.

How can I delete the picture without having to close the form or put the pictures into some kind of delete-queue upon program exit?
 

LebenOjanen

New member
Joined
May 11, 2005
Messages
4
Programming Experience
5-10
Nada

Doesn't work. I still get the standard "Additional information: The process cannot access the file (Picture FIle Name Here) because it is being used by another process." Error

Any idea what could be causing this? So frusturating!
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
post the section(s) of code that "opens" the picture file & the section(s) of code that "deletes" the picture file
 

LebenOjanen

New member
Joined
May 11, 2005
Messages
4
Programming Experience
5-10
Code!

As requested here's the core Open and Delete parts of the code, from their respective functions.

Opening the Image:
VB.NET:
PreviewLoc=App_Path & "images\" & dr("RecipePicture")
PictureName = dr("RecipePicture")
picPreview.Image=System.Drawing.Bitmap.FromFile(PreviewLoc)
Ignore the wierd spacing in the above, not sure why the site is doing it, but PreviewLoc is one word.

Delete the Image:
VB.NET:
PicPreview.Image=Nothing
System.IO.File.Delete(App_Path & "images\" & TmpPicName)
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
Also you could try this:

VB.NET:
{...}
' dispose of the old image and set the picture box to null
If Not (pictureBox1.Image Is Nothing) Then
pictureBox1.Image.Dispose()
End If
pictureBox1.Image = Nothing
IO.File.Delete(fullpathname)
{...}
Cheers ;)
 

LebenOjanen

New member
Joined
May 11, 2005
Messages
4
Programming Experience
5-10
Yay!

Alright, that worked, but if I can keep pestering you, I'd love to know _how_ it worked :) Care to explain?
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
actually it's easy to explain how he did that

he first checked to see if the picture still existed (of which it does) so then he disposes of the image (destroys it in memory) with this line: pictureBox1.Image.Dispose()

then set's the picture box to nothing and deletes the file
 
Top Bottom