469,300 Members | 2,278 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,300 developers. It's quick & easy.

VB.Net: Problem deleting an image used by a PictureBox

krungkrung
hi again to everyone!

I made a simple program(for my VB.Net practice). The program loads an image file to a picturebox upon clicking a button. after loading the image file i have another button to delete the loaded image file from the directory. But I have a problem deleting the image file used by the picturebox even if I set the "PictureBox1.Image = Nothing" before deletion takes place. The error says, "The process cannot access the file because it is being used by another process". What must be the problem with this? can somebody give me an Idea how will i deal with this problem?

Here's my code on delete process:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub RemoveFromDirectory(ByVal strFile As String)
  3.  
  4.         Dim strDirectory As String
  5.  
  6.         strDirectory = Application.StartupPath & "\Images"
  7.  
  8.             If File.Exists(strDirectory & "\" & strFile & ".jpg") = True Then
  9.                 PictureBox1.Image = Nothing
  10.                 File.Delete(strDirectory & "\" & strFile & ".jpg")
  11.             End If
  12.  
  13. End Sub
  14.  
  15.  
**strDirectory --> the image file loaded on a pic box
Nov 17 '08 #1

✓ answered by nukefusion

When you load an image into a picturebox that file is indeed locked. The lock will probably maintain until, in development, you restart your IDE, or in a release environment, your app is restarted.
To get around this, load the image from a file stream object as per the following example:

Expand|Select|Wrap|Line Numbers
  1. Dim fs As System.IO.FileStream
  2. ' Specify a valid picture file path on your computer.
  3. fs = New System.IO.FileStream("C:\WINNT\Web\Wallpaper\Fly Away.jpg", 
  4.      IO.FileMode.Open, IO.FileAccess.Read)
  5. PictureBox1.Image = System.Drawing.Image.FromStream(fs)
  6. fs.Close()
The filestream object should release the lock on the image once closed.

5 22161
nukefusion
221 Expert 100+
When you load an image into a picturebox that file is indeed locked. The lock will probably maintain until, in development, you restart your IDE, or in a release environment, your app is restarted.
To get around this, load the image from a file stream object as per the following example:

Expand|Select|Wrap|Line Numbers
  1. Dim fs As System.IO.FileStream
  2. ' Specify a valid picture file path on your computer.
  3. fs = New System.IO.FileStream("C:\WINNT\Web\Wallpaper\Fly Away.jpg", 
  4.      IO.FileMode.Open, IO.FileAccess.Read)
  5. PictureBox1.Image = System.Drawing.Image.FromStream(fs)
  6. fs.Close()
The filestream object should release the lock on the image once closed.
Nov 17 '08 #2
yeah it works! thanks a lot nukefusion!
Nov 18 '08 #3
Thanks bro , this really helped me
Mar 30 '12 #4
do the following

ceate a batch file like

del <file_name>

name it clear.bat

when you leave your program call it using shell clear.bat

it works in vb 2010
Apr 10 '13 #5
yeah it works! thanks a lot !
Oct 15 '14 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Anup Jishnu via .NET 247 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.