Deleting a file

L

lgbjr

Hello All,

I have a picturebox on a VB.NET form. I use an OpenfileDialog to select an
image for the picturebox (pb.image=image.fromfile(Openfiledialog.fileame)).
Once the image is loaded, I want to delete the file using
file.delete(OpenfileDialog.filename). however, this is throwing an exception
saying that the file is in use by another application.

After I load the image, what do I have to do to release the file so I can
delete it?

Regards,
Lee
 
K

Ken Tucker [MVP]

Hi,

If you dont load an image from a filestream it locks the image.

Dim img As Image

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

img = Image.FromStream(fs)

PictureBox1.Image = img

fs.Close()



Ken

-------------------


Hello All,

I have a picturebox on a VB.NET form. I use an OpenfileDialog to select an
image for the picturebox (pb.image=image.fromfile(Openfiledialog.fileame)).
Once the image is loaded, I want to delete the file using
file.delete(OpenfileDialog.filename). however, this is throwing an exception
saying that the file is in use by another application.

After I load the image, what do I have to do to release the file so I can
delete it?

Regards,
Lee
 
H

Herfried K. Wagner [MVP]

Ken Tucker said:
If you dont load an image from a filestream it locks the image.

Dim img As Image

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

img = Image.FromStream(fs)

PictureBox1.Image = img

fs.Close()

Documentation on 'Image.FromStream':
 
H

Herfried K. Wagner [MVP]

lgbjr said:
After I load the image, what do I have to do to release the file so I can
delete it?

For example:

\\\
Dim b1 As New Bitmap("C:\WINDOWS\Angler.bmp")
Dim b2 As New Bitmap(b1.Width, b1.Height, ...)
Dim g As Graphics = Graphics.FromImage(b2)
g.DrawImageUnscaled(b1, ...)
g.Dispose()
b1.Dispose()
Me.PictureBox1.Image = b2
///
 

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