Releasing a bitmap after use

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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
 
G

Guest

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?
 
M

Mel

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);
}
 
G

Guest

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
 
G

Guest

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);
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

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?
 
I

Ignacio Machin \( .NET/ C# MVP \)

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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

Frans Bouma [C# MVP]

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#)
------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top