Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am writing a program to create an image file say "IMAGE" using the bitmap class of size x,y.
Then I lock the image by using lockbits().
After manipulating the image and unlocking it I saved the image named to a file "IMAGE1" by using the save().

The problem is that even after I unlocked the file "IMAGE" I am not able to delete the file "IMAGE" by help of system.io.fileproperties.delete() command.

But I can Delete the file "IMAGE1" using the same method.


Can anyone PLZ Help me.. :-O
Posted

Without seeing your code it is difficult to comment, but the best guess is that you either:
1) Still have the original "IMAGE" file open.
or
2) Have not disposed all the file access variables for the "IMAGE" file. If you open a file for reading, you must dispose of the stream you read it with. Just deleting the reference to the stream or letting it go out of scope is not sufficient - the Garbage Collector may not dispose of it for hours, days, or months. Until it has been disposed, the file system may well consider it in use and forbid deletes.

The best way to handle this kind of thing is to open the file, read it, close it, and then dispose before you start any processing.
 
Share this answer
 
Comments
PumbaPumba 11-Oct-10 6:01am    
Perfect answer!
C#
Bitmap original = new Bitmap("image.png");
Bitmap clone = (Bitmap)original.Clone();
original.Dispose();
// Unlocks "image.png"
original = null;  // optional
// Now forget original. Work only on the clone.
// ...
clone.Save("image1.png");
File.Delete("image.png");
// Or, directly: clone.Save("image.png");
// Overwrites the original file which is now unlocked);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900