|
-
Apr 13th, 2004, 05:29 PM
#1
Thread Starter
Fanatic Member
FileSystemWatcher: Out Of Memory error
Hi,
I have made a small application to monitor a directory. When I a new BMP file is added to that directory, my program converts it into a jpg file.
I have achieved this by using the FileSystemWatcher class.
Unfortunetely I recieved an 'Out Of Memory' error when a new bitmap file has been added to a folder.
The problem code is below
VB Code:
Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
ListBox1.Items.Add(e.FullPath)
Dim img As Image
img = Image.FromFile(e.FullPath)
'blah blah blah, do something
img.Dispose()
End Sub
I recieve the error on this line
VB Code:
img = Image.FromFile(e.FullPath)
It never happens the first time this sub is called, its always on the second or greater.
Im out of ideas on what it could be, your help is much appreciated
Thanks
Last edited by Geespot; Apr 14th, 2004 at 06:12 AM.
-
Apr 13th, 2004, 07:21 PM
#2
Hyperactive Member
I have had similar problems with the dispose method on images. Try adding a line right under the dispose line that says...
img.image = nothing
That cleared up some problems in my app that were related to memory. I wasn't running out of memory but it was complaining about errors related to memory. I don't remember the exact error.
-
Apr 13th, 2004, 07:22 PM
#3
I wonder how many charact
Image.FromFile has an irky bug within its definition.
Use Image.FromStream instead
VB Code:
Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
ListBox1.Items.Add(e.FullPath)
Dim img As Image
Dim fs As New System.IO.FileStream(e.FullPath,Read)
img = Image.FromStream(fs)
fs.Close
'blah blah blah, do something
img.Dispose()
img =Nothing
End Sub
-
Apr 13th, 2004, 07:51 PM
#4
Sleep mode
Originally posted by scuzymoto
I have had similar problems with the dispose method on images. Try adding a line right under the dispose line that says...
img.image = nothing
That cleared up some problems in my app that were related to memory. I wasn't running out of memory but it was complaining about errors related to memory. I don't remember the exact error.
Dispose method has the effect of setting the object to null . So you don't have to set the object to null after disposal .
-
Apr 14th, 2004, 06:09 AM
#5
Thread Starter
Fanatic Member
Thanks,
Using a stream object did solve the Out Of Memory error, but has produced a new error.
my new code is
VB Code:
'-- A new image as been added, increment counter, this has no purpose!
nThumbnailCount += 1
'-- Get the filename of the new bitmap thats been added, without the BMP extension
Dim sFilename As String = e.FullPath.Substring(0, e.FullPath.Length - 4)
'-- Add a JPG extension to the string
sFilename += ".jpg"
'-- Copy the file, this is because you cant read a file when its in use
System.IO.File.Copy(e.FullPath, "__null.bmp", True)
'-- Open the BMP file in memory
Dim oImg As Image
'-- Read the image into a stream (this avoids the buggy .FromFile, and its faster too)
Dim fs As New IO.FileStream("__null.bmp", IO.FileMode.Open, IO.FileAccess.Read)
oImg = Image.FromStream(fs)
'-- Immediately free up resources from stream, we have finished with it now
fs.Close()
fs = Nothing
'-- Delete the copied file, dont need it now
System.IO.File.Delete("__null.bmp")
'-- Save the image into the JPG file
'oImg.Save(sFilename, Imaging.ImageFormat.Jpeg)
'-- Free up resources
oImg.Dispose()
What Im doing is
- Copying the bitmap into a temporary file
- Opening the new copied file into a Stream
- Loading the stream into the Image object
- Freeing up the stream object
- Deleting the copied bitmap file
- Freeing up the Image object
To replicate the conditions of the program, I am simply pasting (in windows explorer) a bmp file into the folder that the FileSystemWatcher is monitoring, which calls the above code
After the second file that was pasted I get the following error
The process cannot access the file "__null.bmp" because it is being used by another process.
I have narrowed it down to this line
VB Code:
System.IO.File.Copy(e.FullPath, "__null.bmp", True)
Which I dont understand because that file shouldnt exist because my code deletes it.
I thought something like this would of been a walk in the park, guess I am over confident on such matters
Last edited by Geespot; Apr 14th, 2004 at 06:15 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|