Problem Disposing file locks in a PictureBox

  • Thread starter Thread starter Brian Smith
  • Start date Start date
B

Brian Smith

My problem concerns the fact that PictureBoxes appear to retain a lock on
the file supplying the image, and I cannot work out how to release this lock
deterministically (within the Dispose method).

I have a UserControl which contains a PictureBox. On my form I have a Panel
containing a collection of these UserControls. Each has it's image loaded
using Image.FromFile(). My program needs to delete the illustrated files at
certain times, but each File.Delete() attempt causes an exception reporting
File in Use. I have proved conclusively that it is the UI PictureBox control
that is causing this, even though I clear the UI down before I attempt the
Delete.

I've written a lot of what you might assume would be unnecessary Dispose()
code to try and clean up these locks. Can anyone shed light on how this
should be done, PLEASE???

In my UserControl, I have (thus far!):
protected override void Dispose( bool disposing )
{
if ( disposing )
{
// attempt to destroy the PictureBox and all its resources
Controls.Remove(picThumb) ;
picThumb.Image = null ;
picThumb.Click -= new System.EventHandler(this.picThumb_Click);
picThumb.MouseEnter -= new
System.EventHandler(this.picThumb_MouseEnter);
picThumb.Dispose();
picThumb = null;
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

And on my Form, I run this code:
while (panel1.Controls.Count > 0)
{
Control ctl = panel1.Controls[0] ;
panel1.Controls.RemoveAt(0) ;
ctl.Dispose();
}
panel1.Controls.Clear() ;
Application.DoEvents() ;
// now attempt to Delete files...but it will fail.

Brian Smith
 
Load the image using a FileStream instead, and specify in the FileStream
CTOR which share level to use (default is exclusive I believe)

-Paul
 
* "Brian Smith said:
My problem concerns the fact that PictureBoxes appear to retain a lock on
the file supplying the image, and I cannot work out how to release this lock
deterministically (within the Dispose method).

I have a UserControl which contains a PictureBox. On my form I have a Panel
containing a collection of these UserControls. Each has it's image loaded
using Image.FromFile(). My program needs to delete the illustrated files at
certain times, but each File.Delete() attempt causes an exception reporting
File in Use. I have proved conclusively that it is the UI PictureBox control
that is causing this, even though I clear the UI down before I attempt the
Delete.

Use this code for loading the file:

\\\
Dim fs As FileStream = New FileStream("C:\WINDOWS\Angler.bmp", FileMode.Open)
Dim bmp As Bitmap = New Bitmap(fs)
fs.Close()
///
 
Thanks Herfried and Paul. That certainly solves the problem with the file
locks.
The problems I can now see are:
1. memory usage is very high - around 10 MB per file displayed, but I guess
that's down to the size of the bitmaps
2. GC seems very inefficient. If I display 10 images, then clear them
(disposing all PictureBoxes), the memory usage of the program does not
decrease. Displaying some more images just ups the usage by another 100MB
each time.
As it stands, the program would not be viable - it needs to display up to
100 thumbnail images, so I need to extract thumbnail images in a much more
memory-efficient manner if possible - anyone seen any examples of doing
this? One way would be to rescale the bitmap before loading, but since most
image file formats can contain a thumbnail version the best approach would
be to retrieve that.

More importantly, I feel I still lack some understanding on exactly how GC
works, and how to guarantee release of unused memory resources..

brian
 
You might want to dispose of the images explicitly.

pictureBox1.Image.Dispose();
pictureBox1.Image=null;

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm



Brian Smith said:
Thanks Herfried and Paul. That certainly solves the problem with the file
locks.
The problems I can now see are:
1. memory usage is very high - around 10 MB per file displayed, but I guess
that's down to the size of the bitmaps
2. GC seems very inefficient. If I display 10 images, then clear them
(disposing all PictureBoxes), the memory usage of the program does not
decrease. Displaying some more images just ups the usage by another 100MB
each time.
As it stands, the program would not be viable - it needs to display up to
100 thumbnail images, so I need to extract thumbnail images in a much more
memory-efficient manner if possible - anyone seen any examples of doing
this? One way would be to rescale the bitmap before loading, but since most
image file formats can contain a thumbnail version the best approach would
be to retrieve that.

More importantly, I feel I still lack some understanding on exactly how GC
works, and how to guarantee release of unused memory resources..

brian
 
Back
Top