Can't delete an image that is in a picture box

T

Tom Scales

I am having a problem with an application I am writing in vb.net 2003.

I display an image (JPG) on the screen using a Picturebox, loading it with
image.fromfile.

The user can select a key to delete the image.

If I try to delete it, it throws an error that the file is in use.

I tried the image.dispose() method, which allows me to copy the file and
MOST of the time delete the file, but not always. Sometimes it still throws
the in use error.

Any thoughts? Do I need to force garbage collection?

Thanks,

Tom
 
C

Cor Ligthert [MVP]

Tom,

The most sufficient way is for this forever to read first the image in an
image and use that image for your picture box.

If you stream it direct you have to close it and than it is gone.

Cor
 
T

Tom Scales

I can live with it being gone, as I don't need it anymore, but HOW do you
close it?

By read it into an image, do you mean:

Dim tmpImage as image.fromfile("c:\image.jpg")
dim tmpBox as picturebox.image = tmpImage

I have tried every variation of this with no luck.

Tom
 
T

Tom Scales

That looks perfect and seems to work. I can't imagine why the extra step is
necessary, since I am doing a dispose before the delete, but.....

Tom
 
J

james

For whatever reason, when you open and image and load it into a PictureBox,
VB leaves the file locked until the image is no longer being used. Check out
Bob Powell's GDI+ FAQ at this address: http://www.bobpowell.net/faqmain.htm
for a better explaination.
james

Tom Scales said:
That looks perfect and seems to work. I can't imagine why the extra step
is necessary, since I am doing a dispose before the delete, but.....

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top