Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / August 2004

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lucvdv - 30 Jul 2004 09:12 GMT
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.
Lucvdv - 30 Jul 2004 09:21 GMT
> 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?

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")
Lucvdv - 30 Jul 2004 09:31 GMT
> > 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
[quoted text clipped - 10 lines]
>     TWAIN_WriteNativeToFilename(hDib, "temp.bmp")
>     pboxImage.Image = Image.FromFile("temp.bmp")

Or maybe this is safer?

   If Not pboxImage.Image Is Nothing Then
       pboxImage.Image = Nothing
       System.GC.Collect()
   End If
David Williams , VB.NET MVP - 30 Jul 2004 13:10 GMT
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

> > > I just found out that BMP files loaded with Image.FromFile remain
> > > open
[quoted text clipped - 18 lines]
>         System.GC.Collect()
>     End If
Lucvdv - 02 Aug 2004 07:53 GMT
> No, explicitly running Collect() is normally a bad idea - it will
> adversely impact the performance of your code and can cause other
> problems.

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.

> 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.

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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.