Releasing a bitmap after use

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a windows application that contains a PictureBox control. I am using
the statement

PicBox.Image = new Bitmap ("C:\Pic.jpg");

to load a file into the PictureBox to be displayed.

I am using a timer control to rotate the pictures that I display Pic0 - Pic9
every 30 seconds.

However, as I change the picture to the the next one in the sequence, the
old picture is not being "released" and windows will not allow me to edit the
name etc until the entire application is closed down.

How do I drop the connection to the image file when I no longer wish to
display it in my PictureBox so that the file can be edited outside of my
application.

Many thanks in advance
 
Hi,

The image is still in use. Do this, preload all the images in an array then
rotate them to the picturebox

Also I would consider the use of a single animated gif
 
Thanks for your help however pre-loading them will not actually help in my
circumstances. (the example I gave is not my real life example, just a subset
of it)

Is there any way that I can release an image so that it is no longer in use
after I have finished displaying it?
 
Try loading the picture using a filestream

uisng (FileStream fs = new
FileStream(@"C:\Pic.jpg",FileMode.Open,FileAccess.Read,FileShare.Read))
{
PicBox.Image = Image.FromStream(fs);
}
 
Try loading the image from disk as suggested into a new Image object, and
then assign that to the PictureBox's Image property. Then, you can call
Dispose on your original Image object.
Peter
 
Thanks that works perfectly

Mel said:
Try loading the picture using a filestream

uisng (FileStream fs = new
FileStream(@"C:\Pic.jpg",FileMode.Open,FileAccess.Read,FileShare.Read))
{
PicBox.Image = Image.FromStream(fs);
}
 
Hi,

Peter Bromberg said:
Try loading the image from disk as suggested into a new Image object, and
then assign that to the PictureBox's Image property. Then, you can call
Dispose on your original Image object.
Peter

Does this mean that PictureBox makes a copy of the image?
 
Hi,

Mel said:
Try loading the picture using a filestream

uisng (FileStream fs = new
FileStream(@"C:\Pic.jpg",FileMode.Open,FileAccess.Read,FileShare.Read))
{
PicBox.Image = Image.FromStream(fs);
}

The stream will be kept open while the image is in memory.

The only way you can not use the file anymore is if you copy the file to a
MemoryStream and then use Image.FromStream using this memorystream
 
Hi,


Peter Bromberg said:
Ignacio,
Who knows? When I tried it the image didn't disappear so...


Did not disappear from where?

I havent test it but according to the docs the file stay open until the
Image instance is disposed.
 
Ignacio said:
Hi,



Does this mean that PictureBox makes a copy of the image?

Yes, as it will post-process it according to the pictorebox
properties how to display the image (stretched, normal etc.).

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
Back
Top