472,119 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

system IO exception when trying to kill an image file.

Hello,

I am running into a problem with my code and can't seem to figure out the
solution.
I know it has to do with the pciture box control and unloading the image
inthe picture box but I can't seem to figure out the correct way to "unload"
the file.
I have a simple form with a picture box and a button. I am using dir() to
get all the the .tif files and display them in the pciture box. Each time the
button it pressed it displays the next TIF image and then tries to kill() the
previous TIF file. Attempting to kill the TIF file produces an IO exception
that "The process cannot access the file becasue it is being used by another
process".

Here is a sample of the code with the bug:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim docPath As String = "C:\Loader Test\"
If (Button1.Text = "Start") Then
currentFile = Dir(docPath + "*.tif")
If (IsNothing(currentFile) = False) Then
Button1.Text = "Process"
PictureBox1.Image = System.Drawing.Image.FromFile(docPath + currentFile)
Return
Else
'
' message box stating that no files to process
'
Return
End If
End If
'
' Button text box is "Process"
'
PictureBox1.Image = Nothing
newFile = Dir()
'
' Kill current file causes Exception
' This proess cannot access the file becasue
' it is being used by another process.
'
Kill(docPath + currentFile)
currentFile = newFile
PictureBox1.Image = System.Drawing.Image.FromFile(docPath + currentFile)
Return

End Sub
Any answers on how to "unload" the image file.

Thanks

Nov 21 '05 #1
3 2693
In article <C4**********************************@microsoft.co m>,
Sc***@discussions.microsoft.com says...
I am running into a problem with my code and can't seem to figure out the
solution.
I know it has to do with the pciture box control and unloading the image
inthe picture box but I can't seem to figure out the correct way to "unload"
the file.
I have a simple form with a picture box and a button. I am using dir() to
get all the the .tif files and display them in the pciture box. Each time the
button it pressed it displays the next TIF image and then tries to kill() the
previous TIF file. Attempting to kill the TIF file produces an IO exception
that "The process cannot access the file becasue it is being used by another
process".


Try disposing of the Image object used by the PictureBox before
deleting:

PictureBox1.Image.Dispose()

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 21 '05 #2
That did it.

Thanks.

"Patrick Steele [MVP]" wrote:
In article <C4**********************************@microsoft.co m>,
Sc***@discussions.microsoft.com says...
I am running into a problem with my code and can't seem to figure out the
solution.
I know it has to do with the pciture box control and unloading the image
inthe picture box but I can't seem to figure out the correct way to "unload"
the file.
I have a simple form with a picture box and a button. I am using dir() to
get all the the .tif files and display them in the pciture box. Each time the
button it pressed it displays the next TIF image and then tries to kill() the
previous TIF file. Attempting to kill the TIF file produces an IO exception
that "The process cannot access the file becasue it is being used by another
process".


Try disposing of the Image object used by the PictureBox before
deleting:

PictureBox1.Image.Dispose()

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele

Nov 21 '05 #3
"Scott" <Sc***@discussions.microsoft.com> schrieb:
PictureBox1.Image = System.Drawing.Image.FromFile(docPath + currentFile)
[...]
' Kill current file causes Exception
' This proess cannot access the file becasue
' it is being used by another process.
'
Kill(docPath + currentFile)
[...]
Any answers on how to "unload" the image file.


You will have to dispose the 'Image' object in order to be able to delete
the file. To do so, call the object's 'Dispose' method. If you still need
the image inside the application, create a new bitmap based on the original
bitmap.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Michael Zhang | last post: by
2 posts views Thread by Tyler Foreman | last post: by
5 posts views Thread by Henry Stock | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.