Picturebox

P

Paul Hadfield

Hi Guys,

Real simple question here I'm sure. I'm trying to display a JPEG in a
picture box (VB.NET 2003).

pb.Image = Image.FromFile("C:\pic.jpg")

Then, later in the program I need to delete the image file from the disk.

pb.Image = Nothing
IO.File.Delete("C:\pic.jpg")

But I can't as the file is still open/in use. If I try to delete the image
file without displaying it in the picture box first it deletes fine, so I
know it's the picture box that's still got hold of the image file somehow.

What am I missing?


Cheers,
Paul.
 
K

Ken Tucker [MVP]

Hi,

Dim fs As New System.IO.FileStream("C:\bliss.jpg", IO.FileMode.Open)

Dim bm As New Bitmap(fs)

PictureBox1.Image = DirectCast(bm.Clone, Image)

fs.Close()

bm.Dispose()

System.IO.File.Delete("C:\bliss.jpg")



Ken
 
H

Herfried K. Wagner [MVP]

Paul Hadfield said:
Real simple question here I'm sure. I'm trying to display a JPEG in a
picture box (VB.NET 2003).

pb.Image = Image.FromFile("C:\pic.jpg")

Then, later in the program I need to delete the image file from the disk.

pb.Image = Nothing
IO.File.Delete("C:\pic.jpg")

But I can't as the file is still open/in use. If I try to delete the image
file without displaying it in the picture box first it deletes fine, so I
know it's the picture box that's still got hold of the image file somehow.

Check out the code listings at
<URL:http://dotnet.mvps.org/dotnet/code/graphics/#ImageNoLock>.
 
P

Paul Hadfield

Thanks Ken - perfect.

Ken Tucker said:
Hi,

Dim fs As New System.IO.FileStream("C:\bliss.jpg", IO.FileMode.Open)

Dim bm As New Bitmap(fs)

PictureBox1.Image = DirectCast(bm.Clone, Image)

fs.Close()

bm.Dispose()

System.IO.File.Delete("C:\bliss.jpg")



Ken
 
C

Cerebrus

Hi Herfried,

Herfried wrote :

I've been meaning to ask you this for some time now... Do you have an
English version of your site ? There's so much information there that I
would like to learn, but the language barrier ... ! I did do a course
in German language many years back, but my German is too rusty to be
able to understand technicalities. ;-)

Thanks,

Cerebrus.
 
H

Herfried K. Wagner [MVP]

Cerebrus said:
I've been meaning to ask you this for some time now... Do you have an
English version of your site ? There's so much information there that I
would like to learn, but the language barrier ... ! I did do a course
in German language many years back, but my German is too rusty to be
able to understand technicalities. ;-)

Sorry, but I currently do not plan an English translation. However, the
tips in the FAQ collection are available in English!
 

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