all groups > dotnet clr > july 2004 >
You're in the

dotnet clr

group:

System.Drawing.Image.FromFile leaves file open longer than necessary: bug?


Re: System.Drawing.Image.FromFile leaves file open longer than necessary: bug? David Williams , VB.NET MVP
7/30/2004 5:10:52 AM
dotnet clr:
No, explicitly running Collect() is normally a bad idea - it will
adversely impact the performance of your code and can cause other
problems.

The best way that I have found to handle this problem is to load the
image from a stream object that you control instead of directly from a
file. This way you can use the image as long as you need, but close and
dispose the stream as soon as you are done reading it.

HTH

David

[quoted text, click to view]
System.Drawing.Image.FromFile leaves file open longer than necessary: bug? Lucvdv
7/30/2004 10:12:29 AM
I just found out that BMP files loaded with Image.FromFile remain open
until my application terminates. Is this a bug, or do I have to do
something to make sure the file is closed?


For example if this VB code (in a button click handler) is executed twice,
the delete fails on the second pass. After restarting the program it
succeeds once, on the second pass it fails again.

Picturebox1.Image = Nothing
If System.IO.File.Exists("temp.bmp") Then _
System.IO.File.Delete("temp.bmp")
TWAIN_WriteNativeToFilename(hDib, "temp.bmp")
PictureBox1.Image = Image.FromFile("temp.bmp")

TWAIN_Write... saves a bitmap obtained from a TWAIN source to a file.

I verified that Image.FromFile is causing it by commenting that line out:
the error stopped occurring.


I also tested this:

PictureBox1.Image = Image.FromFile(Filename1)
PictureBox1.Image = Image.FromFile(Filename2)

Filename1 remains open (can't be deleted) until after the application has
terminated.
Re: System.Drawing.Image.FromFile leaves file open longer than necessary: bug? Lucvdv
7/30/2004 10:21:00 AM
[quoted text, click to view]

Explicitly disposing the image seems to fix it:

If Not pboxImage.Image Is Nothing Then
pboxImage.Image.Dispose()
pboxImage.Image = Nothing
End If
If System.IO.File.Exists("temp.bmp") Then _
System.IO.File.Delete("temp.bmp")
TWAIN_WriteNativeToFilename(hDib, "temp.bmp")
pboxImage.Image = Image.FromFile("temp.bmp")
Re: System.Drawing.Image.FromFile leaves file open longer than necessary: bug? Lucvdv
7/30/2004 10:31:58 AM
[quoted text, click to view]

Or maybe this is safer?

If Not pboxImage.Image Is Nothing Then
pboxImage.Image = Nothing
System.GC.Collect()
End If
Re: System.Drawing.Image.FromFile leaves file open longer than necessary: bug? Lucvdv
8/2/2004 8:53:49 AM
On Fri, 30 Jul 2004 05:10:52 -0700, "David Williams , VB.NET MVP"
[quoted text, click to view]

That may depend on the situation. I found out in a DirectX application
that has to run for long stretches of time (12 hours or more of
uninterrupted D3D rendering in an applications that's controlled by events
it receives at a constant rate of several events/second), it's better to
force it at regular small intervals to prevent it from causing a noticeable
load on the system when it finally does it by itself.

[quoted text, click to view]

I noticed it in a test program I wrote to find the best way to handle the
TWAIN capture, but the real app saves and reads small jpegs into/from a
database (ID photographs in a people database). I was already planning to
load them into a byte array for doing the database side and make a memory
stream out of the same array for copying into the picturebox, so that would
automatically become the solution.
AddThis Social Bookmark Button