Picture box and deleting file

  • Thread starter Thread starter FrankB
  • Start date Start date
F

FrankB

Hello,

after setting another image to the picture box it is not possible to
delete the last shown file via File.Delete ( sFilename). Path of image
file is correct. Error message box says: file is being used by other
process.

Is there a way to "free" the file?

Thank you for your help.

Frank
 
You have to dispose the Image instance in the PictureBox first.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
Hi Kevin,

thank you for your help. Unfortunately a Dispose on the image does not
change anything. There is still the same message box.

Perhaps my code helps to understand the problem:

pictureBoxMain.Image = Image.FromFile ( sFile );

...

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

try
{
File.Delete ( sFile );
}


Sorry for bothering again...

Frank
 
You have to dispose the Image instance in the PictureBox first.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:http://www.miradyne.net

the dispose is the way, you should try to call the GarbageCollector be
more shure that the reference is eliminated
 
after setting another image to the picture box it is not possible to
delete the last shown file via File.Delete ( sFilename). Path of image
file is correct. Error message box says: file is being used by other
process.

Is there a way to "free" the file?

As long as the Image is still around somewhere (eg attached to the
PictureBox), you're not going to be able to do anything with the file. I
assume that this is because .NET has memory-mapped the file, so that the
backing store is the original file rather than the swap file. At least,
that's the only reason I can think of for it to keep the file locked after
creating the Image instance.

Anyway, the solution is to make a copy of the image before attaching to
the PictureBox:

using (Image image = Image.FromFile(...))
{
pictureBoxMain.Image = new Image(image);
}

// deleting original image file should work at this point in the code

Pete
 
FrankB said:
Hello,

after setting another image to the picture box it is not possible to
delete the last shown file via File.Delete ( sFilename). Path of image
file is correct. Error message box says: file is being used by other
process.

Is there a way to "free" the file?

Why not first read the file into a memory stream, then create the image from
that memory stream? In this way, you can close the file right after you have
read the image data into your memory stream.

Regards,
Christian Stapfer
 
If you have picture box pb you need to dispose not the box, but the image.
For me this works:

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

and then Delete on file.

But you must be pretty sure no other application is having the file open

HTH
Alex
 

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

Back
Top