PictureBox Control releasing files

  • Thread starter Thread starter Sean
  • Start date Start date
S

Sean

Hello all,

I have an application that is using some picture box controls that are being
used to preview jpg files. The jpg files are created on the fly and will be
overwritten when needed. The issue I'm having is that when I release the
picturebox resources, they still seem to be hanging on at the OS level
because I can't delete the file.

Here's a code snippet of what I'm doing:

private void button1_Click(object sender, System.EventArgs e)
{
//check to see if the picturebox has an image already.
if(pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
if(System.IO.File.Exists("C:\\test.jpg"))
{
System.IO.File.Delete("C:\\test.jpg");
}
//Save the new file with the same name:
wwss.SaveAsJpeg("C:\\test.jpg");
pictureBox1.Image =
Image.FromFile(@"C:\\test.jpg").GetThumbnailImage(136,120,null,new
intPtr());
}

Works first time through every time. The second time the button is clicked,
it goes into the check for image section just fine. No errors, until I get
to deleting the file. If I comment out the pictureBox1.Image =
Image.FromFile... section and trace through this same code, it'll run all
day long deleting the file and saving a new one. So where am I going wrong,
or is this some known issue that I'm not aware of ? Using .NET 1.0

Thanks!

Sean
 
see comments below
Sean said:
Hello all,

I have an application that is using some picture box controls that are being
used to preview jpg files. The jpg files are created on the fly and will be
overwritten when needed. The issue I'm having is that when I release the
picturebox resources, they still seem to be hanging on at the OS level
because I can't delete the file.

Here's a code snippet of what I'm doing:

private void button1_Click(object sender, System.EventArgs e)
{
//check to see if the picturebox has an image already.
if(pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
if(System.IO.File.Exists("C:\\test.jpg"))
{
System.IO.File.Delete("C:\\test.jpg");
}
//Save the new file with the same name:
wwss.SaveAsJpeg("C:\\test.jpg");
pictureBox1.Image =
Image.FromFile(@"C:\\test.jpg").GetThumbnailImage(136,120,null,new
intPtr());
}

The problem is that you have two image objects and you diposing the
wrong one. Let me show you in more detail:

This line:
pictureBox1.Image =
Image.FromFile(@"C:\\test.jpg").GetThumbnailImage(136,120,null,new
intPtr());

Is more or less the same as:
Image image1 = Image.FromFile(@"C:\\test.jpg")
Image image2 = image1.GetThumbnailImage(blah, blah)
pictureBox1.Image = image2;
image1 = null; // I don't believe there would be any references to
the original image, so it's like it's null here

However, just because image1 is "null" doesn't mean that it has been
disposed. I suppose if a GC happened to get it, you'd be fine. I'd
suggest the following:

using (Image image1 = Image.FromFile(@"C:\\test.jpg"))
{
pictureBox1.Image = image1.GetThumbnailImage(blah, blah)
} // note that dispose is automatically called as you leave this block

I didn't actually test it, cause I don't feel like implementing the
callback and all that garbage. You probably won't even need all your
other code as follows:
if(pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}

Let me know if there are any other issues.

regards,
Mike Mayer
 
Mike,

Worked like a champ! Thanks alot. Just needed another pair of eyes and had
a horrible brain block.

Sean
 
Back
Top