Deleting images after viewed thru a picturebox

B

Bart Steur

Hi,

I'm writing an app to maintain products. The products are listed in a
listbox and when I click a product in a listbox some info of that product is
shown including a picture of the product.

I do this by using the image.fromfile function: pbProduct.Image =
Image.FromFile(myImagePath & productkey & ".jpg")

Now I'm building the option to delete a product. When a product is selected
in the listbox a Delete button is activated. Pressing the Delete Button
deletes the product row from the database and should delete the picture from
the picturefolder, but.... It can't because it says it's in use. It's in use
by the picturebox (pbProduct). I've tested that because when I don't load
the picture when selecting the product, I can delete it (database row +
image file)

I've tried the pbProduct.Image.Dispose, setting the image to Nothing, but
none of it seems to work. Even loading a different image into the picture
box before deleting. Nothing worked.

Any ideas?

Thanks
Bart
 
C

Chris Diver

Hi,

I just tried your re-create your problem and pbProduct.Image.Dispose()
worked fine for me.

Is it possible that when you tried to dispose it then you loaded it more
than once, i.e. if your listbox event just performs that line then if
you click the same product more than once then you are loading it twice
without disposing the first one.

Try adding this before pbProduct.Image = Image.FromFile(myImagePath &
productkey & ".jpg")

Try
pbProduct.Image.Dispose()
Catch
End Try

HTH
Chris.
 
H

Herfried K. Wagner [MVP]

Bart Steur said:
I'm writing an app to maintain products. The products are listed in a
listbox and when I click a product in a listbox some info of that product
is shown including a picture of the product.

I do this by using the image.fromfile function: pbProduct.Image =
Image.FromFile(myImagePath & productkey & ".jpg")

Now I'm building the option to delete a product. When a product is
selected in the listbox a Delete button is activated. Pressing the Delete
Button deletes the product row from the database and should delete the
picture from the picturefolder, but.... It can't because it says it's in
use. It's in use by the picturebox (pbProduct).

\\\
Using OldImage As Image = Me.PictureBoxProduct.Image
Me.PictureBoxProduct.Image = My.Resources.DefaultProductImage
End Using
Kill(<file name>)
///
 

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