By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,351 Members | 1,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,351 IT Pros & Developers. It's quick & easy.

Picture box and deleting file

P: n/a
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

Jun 14 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

"FrankB" <bo*******@iabg.dewrote in message
news:11**********************@d30g2000prg.googlegr oups.com...
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

Jun 14 '07 #2

P: n/a
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

Jun 14 '07 #3

P: n/a
On 14 jun, 07:19, "Kevin Spencer" <unclechut...@nothinks.comwrote:
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

"FrankB" <boedde...@iabg.dewrote in message

news:11**********************@d30g2000prg.googlegr oups.com...
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
the dispose is the way, you should try to call the GarbageCollector be
more shure that the reference is eliminated

Jun 14 '07 #4

P: n/a
On Thu, 14 Jun 2007 04:09:52 -0700, FrankB <bo*******@iabg.dewrote:
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
Jun 14 '07 #5

P: n/a
"FrankB" <bo*******@iabg.dewrote in message
news:11**********************@d30g2000prg.googlegr oups.com...
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

Jun 14 '07 #6

P: n/a
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

"FrankB" <bo*******@iabg.dewrote in message
news:11**********************@d30g2000prg.googlegr oups.com...
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

Jun 15 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.