File access denied

G

Guest

When I run my program, I open a picturebox with a photo loaded in to it using
the code:
PictureBox1.Image = Image.FromFile(strPath & strImage).
I then close the picture box and put in the line:
PictureBox1.Dispose()
and it still will not let me delete the file which the photo is located. It
gives me the in use error message. I need to be able to delete the photo
without closing the program. Can anyone help?
 
B

Barry Kelly

Mark said:
When I run my program, I open a picturebox with a photo loaded in to it using
the code:
PictureBox1.Image = Image.FromFile(strPath & strImage).
I then close the picture box and put in the line:
PictureBox1.Dispose()
and it still will not let me delete the file which the photo is located. It
gives me the in use error message. I need to be able to delete the photo
without closing the program. Can anyone help?

The picture box doesn't dispose its image, so by disposing the picture
box you're not disposing the image. To guarantee that the image file is
closed, I recommend you create a separate FileStream in a Using
statement and load the image from that stream rather than passing a file
name. That way, the file stream definitely gets closed.

-- Barry
 
G

Guest

Thanks for your suggestion, but I am new at this and I don't fully
understand. Can you give me a sample, please. Thanks in advance.
 
B

Barry Kelly

Mark said:
Thanks for your suggestion, but I am new at this and I don't fully
understand. Can you give me a sample, please. Thanks in advance.

Somewhat like this (in C#):

---8<---
PictureBox pbox;

// ...

using (Stream stream = File.OpenRead("your\\path\\here"))
{
pbox.Image = Image.LoadFromStream(stream);
}
--->8---

Look up the Using statement in VB for VB syntax.

-- Barry
 
B

Barry Kelly

Barry Kelly said:
Somewhat like this (in C#):

---8<---
PictureBox pbox;

// ...

using (Stream stream = File.OpenRead("your\\path\\here"))
{
pbox.Image = Image.LoadFromStream(stream);

Make that FromStream, not LoadFromStream
}
--->8---

Look up the Using statement in VB for VB syntax.

-- Barry

-- Barry
 

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