Renaming or deleting a displayed imgae
-
How do I release the resources for an image so I can rename or delete it. Bitmap pic = new Bitmap(infile); pictureBox1.Image = pic; // A button is clicked to rename or delete the picture // I have tried pic=null; pictureBox1.Image=null; File.Move(inpic, outpic); //I get ************** Exception Text ************** //System.IO.IOException: The process cannot access the file because it is //being used by another process. I have seen this issue dealt with before but have not been able to find where.
-
How do I release the resources for an image so I can rename or delete it. Bitmap pic = new Bitmap(infile); pictureBox1.Image = pic; // A button is clicked to rename or delete the picture // I have tried pic=null; pictureBox1.Image=null; File.Move(inpic, outpic); //I get ************** Exception Text ************** //System.IO.IOException: The process cannot access the file because it is //being used by another process. I have seen this issue dealt with before but have not been able to find where.
electriac wrote:
How do I release the resources for an image so I can rename or delete it.
Dispose it!
pic.Dispose();
pic=null;GDI+ keeps the image stream open until you dispose the image. It's buried way deep in the documentation - like a one sentence blurb in an overview somewhere. :) You can work with a clone as well, like Christian mentioned. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
How do I release the resources for an image so I can rename or delete it. Bitmap pic = new Bitmap(infile); pictureBox1.Image = pic; // A button is clicked to rename or delete the picture // I have tried pic=null; pictureBox1.Image=null; File.Move(inpic, outpic); //I get ************** Exception Text ************** //System.IO.IOException: The process cannot access the file because it is //being used by another process. I have seen this issue dealt with before but have not been able to find where.
The problem is that the picture was never Disposed. There are also bugs in the framework, I find the best thing is to create the bitmap, make a copy, work with the copy, and call Dispose on the original object. Setting to null doesn't do much, although it can increase the time before GC collects it, there's no immediate effect on the object.
Christian Graus Driven to the arms of OSX by Vista. "Iam doing the browsing center project in vb.net using c# coding" - this is why I don't answer questions much anymore. Oh, and Microsoft doesn't want me to.
-
electriac wrote:
How do I release the resources for an image so I can rename or delete it.
Dispose it!
pic.Dispose();
pic=null;GDI+ keeps the image stream open until you dispose the image. It's buried way deep in the documentation - like a one sentence blurb in an overview somewhere. :) You can work with a clone as well, like Christian mentioned. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
electriac wrote:
How do I release the resources for an image so I can rename or delete it.
Dispose it!
pic.Dispose();
pic=null;GDI+ keeps the image stream open until you dispose the image. It's buried way deep in the documentation - like a one sentence blurb in an overview somewhere. :) You can work with a clone as well, like Christian mentioned. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
I am certain that there have been times that I've disposed of the image and it's still held onto the file, depending on how it was opened. But, yeah, as we both said, Dispose is the underlying solution that the OP was missing.
Christian Graus Driven to the arms of OSX by Vista. "Iam doing the browsing center project in vb.net using c# coding" - this is why I don't answer questions much anymore. Oh, and Microsoft doesn't want me to.
-
I am certain that there have been times that I've disposed of the image and it's still held onto the file, depending on how it was opened. But, yeah, as we both said, Dispose is the underlying solution that the OP was missing.
Christian Graus Driven to the arms of OSX by Vista. "Iam doing the browsing center project in vb.net using c# coding" - this is why I don't answer questions much anymore. Oh, and Microsoft doesn't want me to.
-
I am certain that there have been times that I've disposed of the image and it's still held onto the file, depending on how it was opened. But, yeah, as we both said, Dispose is the underlying solution that the OP was missing.
Christian Graus Driven to the arms of OSX by Vista. "Iam doing the browsing center project in vb.net using c# coding" - this is why I don't answer questions much anymore. Oh, and Microsoft doesn't want me to.
Christian Graus wrote:
I am certain that there have been times that I've disposed of the image and it's still held onto the file
Yup - I've seen this. It seems to depend (to a certain extent) on how the image was opened in the first place.
Deja View - the feeling that you've seen this post before.
-
Christian Graus wrote:
I am certain that there have been times that I've disposed of the image and it's still held onto the file
Yup - I've seen this. It seems to depend (to a certain extent) on how the image was opened in the first place.
Deja View - the feeling that you've seen this post before.
Pete O'Hanlon wrote:
It seems to depend (to a certain extent) on how the image was opened in the first place.
That makes sense. Disposing the Bitmap to make it release the file only works if it's actually the Bitmap that is holding on to the file. The Bitmap should always be disposed, of course, even if it's not to release the file.
Despite everything, the person most likely to be fooling you next is yourself.
-
Christian Graus wrote:
I am certain that there have been times that I've disposed of the image and it's still held onto the file
Yup - I've seen this. It seems to depend (to a certain extent) on how the image was opened in the first place.
Deja View - the feeling that you've seen this post before.
Yeah "Deja View" I know I have seen this issue before but after spending many hours with Google etc. to no avail I resorted to asking for help. Many thanks Code Project. I try not to ask questions unless I have exhausted all alternatives. Thanks for your help I now have greater insight into the issue than my simple problem required.
-
electriac wrote:
How do I release the resources for an image so I can rename or delete it.
Dispose it!
pic.Dispose();
pic=null;GDI+ keeps the image stream open until you dispose the image. It's buried way deep in the documentation - like a one sentence blurb in an overview somewhere. :) You can work with a clone as well, like Christian mentioned. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Yeah be very careful with anything that wraps GDI functionality. Even innocent looking things like Matrix implement IDisposable. :( Check, double-check, etc.
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games. -
Hey there, it was my post about a month ago you're talking about:) Glad you found the solution:), One more problem is in the pictureBox control "when loading it with a bitmap", more details are on msdn website..
All generalizations are wrong, including this one! (\ /) (O.o) (><)
-
Yeah "Deja View" I know I have seen this issue before but after spending many hours with Google etc. to no avail I resorted to asking for help. Many thanks Code Project. I try not to ask questions unless I have exhausted all alternatives. Thanks for your help I now have greater insight into the issue than my simple problem required.
electriac wrote:
I know I have seen this issue before but after spending many hours with Google etc
That's the spirit. BTW - don't think that Deja View referred to your problem. It's part of my sig.
Deja View - the feeling that you've seen this post before.
-
Hey there, it was my post about a month ago you're talking about:) Glad you found the solution:), One more problem is in the pictureBox control "when loading it with a bitmap", more details are on msdn website..
All generalizations are wrong, including this one! (\ /) (O.o) (><)
-
electriac wrote:
I know I have seen this issue before but after spending many hours with Google etc
That's the spirit. BTW - don't think that Deja View referred to your problem. It's part of my sig.
Deja View - the feeling that you've seen this post before.
-
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/91bffae9-c78e-458b-b23c-42cee71f7669/[^]
All generalizations are wrong, including this one! (\ /) (O.o) (><)
Muammar Thanks for the link. I am doing a rewrite of my media player which displays images associated with the MP3 files. Since the program also deletes, renames, and exports groups of files I have had some difficulty do to the problem of unreleased resources. I believe that with the help of all you "Code Project" people I have everything working now. Thanks to all Electriac
-
I did think it was a cute reference to my inability to find the previous post about picture boxes.
No - if you look at my profile history, you'll see this in all my posts. I've been using it for 3 years now.
Deja View - the feeling that you've seen this post before.