Hi...
I wrote code using vb.net
picturebox1.image=image.Fromfile("C:\a.jpg")
picturebox1 display image a.jpg.

if picturebox1 display, file a.jpg can't delete it because "being used by another person".
how to close picturebox1 so file a.jpg can delete?

i have tried to use picturebox1.dispose() and picturebox1.image=nothing but still can delete and display error message "being used by another person"

thanx

Dani AI

Generated

— the symptom is the usual GDI+ file-handle behavior: the image loader that opens a file can keep the OS handle until the Image object is released, so Windows prevents rename/delete. was right that disposing the image is the key; ’s note about disposing the control “working” is true but heavy-handed. A more robust pattern is to read the file into memory and assign a standalone Bitmap copy to the PictureBox so the on-disk file is closed immediately.

Dim path As String = "C:\a.jpg"
Dim data() As Byte = System.IO.File.ReadAllBytes(path)

Using ms As New System.IO.MemoryStream(data)
    Dim srcImg As System.Drawing.Image = System.Drawing.Image.FromStream(ms)
    Dim bmpCopy As New System.Drawing.Bitmap(srcImg)

    Dim previous = myPictureBox.Image
    myPictureBox.Image = bmpCopy
    If previous IsNot Nothing Then previous.Dispose()
End Using

Notes: this releases the file immediately (you can delete/rename right after). If images are large or many, watch memory usage and dispose images when a form closes. If deletion still fails, check for other references (other threads, components, or external apps like antivirus) that might hold the file. This approach works across .NET versions and is safer than trying to force-dispose the control itself.

Recommended Answers

All 7 Replies

Learn about Resources.

Hi...
I wrote code using vb.net
picturebox1.image=image.Fromfile("C:\a.jpg")
picturebox1 display image a.jpg.

if picturebox1 display, file a.jpg can't delete it because "being used by another person".
how to close picturebox1 so file a.jpg can delete?

i have tried to use picturebox1.dispose() and picturebox1.image=nothing but still can delete and display error message "being used by another person"

thanx

Hi,
you need to use :- PictureBox1.Image.Dispose() by using dispose on the picturebox, you are not disposing the image itself.

Hi,
you need to use :- PictureBox1.Image.Dispose() thanx for your reply,
by using dispose on the picturebox, you are not disposing the image itself.

Picturebox1.image.dispose() can't delete and display error
if user vb 2005,2008 can use picturebox1.image=nothing to close and delete/rename that image file.
but i have tried use vb 2003 and result can't rename that image file

i use vb.2003
thanx

i m facing the same in vb.net 2008

i think it's just like when you're opening a file using file stream that's why it prompts the message that it's being used..

try
then after closing, delete the file

hope this helps,

i have found some different and it is working.
i.e.

Use picturebox1.dispose() instead of picturebox1.image.dispose().

I need to determine which tab the user is coming from, and going to, when they switch tabs, and possibly cancel the switch. I have tried the Deselecting, Deselected, Selecting, Selected events, and all of them show the e.TabPageIndex to be the same as the sender.SelectedIndex.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.