By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

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

krungkrung
P: 18
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.

Share this Question
Share on Google+
5 Replies


nukefusion
Expert 100+
P: 221
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

krungkrung
P: 18
yeah it works! thanks a lot nukefusion!
Nov 18 '08 #3

P: 1
Thanks bro , this really helped me
Mar 30 '12 #4

P: 1
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

P: 1
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.