File access denied

  • Thread starter Thread starter Guest
  • Start date Start date
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?
 
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
 
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.
 
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
 
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
 
Back
Top