473,657 Members | 2,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim docPath As String = "C:\Loader Test\"
If (Button1.Text = "Start") Then
currentFile = Dir(docPath + "*.tif")
If (IsNothing(curr entFile) = False) Then
Button1.Text = "Process"
PictureBox1.Ima ge = 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.Ima ge = 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.Ima ge = System.Drawing. Image.FromFile( docPath + currentFile)
Return

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

Thanks

Nov 21 '05 #1
3 2811
In article <C4************ *************** *******@microso ft.com>,
Sc***@discussio ns.microsoft.co m 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.Ima ge.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************ *************** *******@microso ft.com>,
Sc***@discussio ns.microsoft.co m 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.Ima ge.Dispose()

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

Nov 21 '05 #3
"Scott" <Sc***@discussi ons.microsoft.c om> schrieb:
PictureBox1.Ima ge = 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5815
by: JW | last post by:
Hi, I am trying to see if I can display an animated .gif file on a Java application (that is, I do not want to have to show a series of static images to simulate animateion; I just want to display a file that is of the animated .gif type). I was able run the following code (2 java classes) on a Windows 2000 machine using Java 1.4 and it works fine,
2
4383
by: Michael Zhang | last post by:
My project uses Python-2.3.4 + Tkinter + PIL-1.1.4 to retrieve images from server and display those images. I created a thread (also a separate toplevel window) for displaying images and another thread for recording the frame rates (using a progress bar for visulization). The whole application worked very well once it received image data from the socket. The problem is when I tried to close that display window (click on the standard...
3
20068
by: anastasia | last post by:
I get an out of memory exception when attempting to excecute the following code: original = System.Drawing.Image.FromFile(file.FileName,true); I ONLY get this exception when the file is in the "My Documents" folder or subfolders. If the file lives anywhere else on the hard drive, I have no problems. What could be going on here?
2
5954
by: Tyler Foreman | last post by:
Hello, I had an earlier post regarding a strange exception I (thought) I was getting in my windows form. I have since been able to trace the problem to a picturebox control on my form. I can reproduce the exception consistently, but it seems to be timing dependent. Here is what happens: I have a subform that I open, load an image from a file into my PictureBox, take some user input, then I hide the
1
8144
by: Mchuck | last post by:
I've seen several newsgroup topics everywhere concerning this, as well as a couple of articles from the MSDN website, but this error still baffles me. It has to do with using the Image.FromStream(...) function to load an image into a PictureBox control from a byte array received from an SQL Server database. Here's a snippet of what I've been trying to achieve: ----------------------------
3
4003
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user selects? thanks...
5
8312
by: Henry Stock | last post by:
I am trying to understand the following error: Any thing you can tell me about this is appreciated. Security Exception Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file. Exception Details: for the permission of type
2
3269
by: ouzsharp | last post by:
The follwing class works well when used in a C# console application: namespace ScreenShotDemo { /// <summary> /// Provides functions to capture the entire screen, or a particular window, and save it to a file. /// </summary> public class ScreenCapture {
3
5886
by: maneshborase | last post by:
Hi friends, I am facing one serious problem in my application. I am trying to open dicom image file (.dcm) has size around 400 MB. But I am getting and unhandy exceptions, Some time, {System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at...
0
8397
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7333
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6167
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.