Click here to Skip to main content
15,917,793 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
bitmap occupies the file "targetImage.jpg". Please see the code below that presents the real problem I am facing. I have created a separate program to clarify the problem.
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace ALL_RFID_PROJECTS_1._0._2
{
    static class PROGRAM
    {
        static void Main()
        {
            Bitmap b = new Bitmap(@"D:\01.jpg");
            System.IO.File.Delete(@"D:\01.jpg");
        }


    }
}

The Exception Message is="The process cannot access the file 'D:\01.jpg' because it is being used by another process."
How can I release the file from bitmap so I can delete it.
Please help me.
Posted
Comments
Toli Cuturicu 4-Dec-10 4:59am    
You should not (and I mean DON'T) use all capitals class names.
All capitals identifiers are very seldom used, almost never (sometimes for some constants in pinvoke scenarios).

If you call dispose on the bitmap, it can be deleted without any problems.

Bitmap b = new Bitmap(@"D:\01.jpg");
b.Dispose(); // now the bitmap is released
System.IO.File.Delete(@"D:\01.jpg");


Cheers

Manfred
 
Share this answer
 
Comments
J imran 3-Dec-10 2:28am    
exactly , just seen the msdn and found it my self hehe.
Thankk you boss.
Ankur\m/ 3-Dec-10 2:36am    
Is the first line of code at all necessary?
Ankur\m/ 3-Dec-10 2:43am    
I was more concerned with the error that OP was getting. And I knew removing the first line should solve it.
Anyways thanks for the information and the vote.
Ankur\m/ 3-Dec-10 4:26am    
"I thought you can vote only once for an answer, but when I clicked again the vote changed. Did I miss anything?"
This is by design. CP allows you to re-vote and the most recent vote is considered.
And I strongly feel this is how it should be. Consider this situation for example. You felt the answer was not correct and down-voted me. After I clarified myself, you wanted to reconsider your vote. This is the most important reason why this feature there. Btw, Thanks for reconsidering your vote. And you know, you get a point too when you up-vote an answer (rather anything).
Cheers!
Ankur\m/ 3-Dec-10 4:27am    
Btw, take a 5 for a better answer. :)
Remove the first line:
C#
Bitmap b = new Bitmap(@"D:\01.jpg");

and try again.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 3-Dec-10 2:33am    
The question was what to do so the file could be deleted. b.Dispose() solves the issue, not removing the Bitmap relevant code.
Manfred Rudolf Bihy 3-Dec-10 2:39am    
As OP stated the program was solely created to clarify his problem. To make that clearer in the code he pasted maybe he should have written something like "//Doing stuff with b here" after the line where the bitmap was created
Manfred Rudolf Bihy 3-Dec-10 2:48am    
Ok, I'll make up for that down vote, on one of your next posts. :)
Manfred Rudolf Bihy 3-Dec-10 2:50am    
Hey this is interesting. I thought you can vote only once for an answer, but when I clicked again the vote changed. Did I miss anything?
C#
Bitmap temp = new Bitmap(@"D:\01.jpg");
Bitmap b = (Bitmap)temp.Clone();
temp.Dispose(); // now temp is released
System.IO.File.Delete(@"D:\01.jpg");
// you may safely use b from now on and forget about temp...

And, please, don't put any files in the root directory of drives... You may get in trouble in Windows Vista / Seven with that! Use subdirectories instead.
 
Share this answer
 
Comments
fjdiewornncalwe 3-Dec-10 11:56am    
The answer was given above over nine hours before yours. Please read the existing answers first. If the answer is the same, just add a comment to it to add your extra comment. You're not wrong, persay, but it is simply bad-form to "hijack" answers.
Toli Cuturicu 3-Dec-10 13:08pm    
You are not being focused and you are wrong.
My answer is very, very different, and, in fact, it is the only usable one.
If the bitmap in not cloned before being disposed, it cannot be used afterwards, which defeats the purpose of it being declared in the first place.
Toli Cuturicu 3-Dec-10 13:10pm    
And, because you erroneusly downvoted it, I reposted the answer!
Manfred Rudolf Bihy 3-Dec-10 18:19pm    
Hi Toli, I have an issue with this post. As I had anticipated that the bitmap should be somehow used, my first try was also to call Clone() on the bitmap. In my test program this resulted in the file not being deletable eventhough the original bitmap (the one that was cloned) was properly disposed. I'm asking you, have you really tried what you posted? I found that calling b = new Bitmap(temp); and then disposing temp allowed me to delete the file that bitmap temp was build from. I'm not commenting on this to aggravate you. I just wanted to make sure that I didn't misinterpret my findings. :)
Toli Cuturicu 4-Dec-10 4:55am    
I have similar code in an working software (a lite image viewer).
And who is Marcus Kramer? Your lawyer?
C#
Bitmap temp = new Bitmap(@"D:\01.jpg");
Bitmap b = (Bitmap)temp.Clone();
temp.Dispose(); // now temp is released
System.IO.File.Delete(@"D:\01.jpg");
// you may safely use b from now on and forget about temp...
And, please, don't put any files in the root directory of drives... You may get in trouble in Windows Vista / Seven with that! Use subdirectories instead.
 
Share this answer
 
Comments
Toli Cuturicu 3-Dec-10 13:12pm    
Now justice has been made!
Manfred Rudolf Bihy 3-Dec-10 19:43pm    
OK, I think I should probably not say this, but I just can't resist:
Justice can't be made, justice is done.
Please forgive me :)
Toli Cuturicu 4-Dec-10 4:52am    
I am not an English native speaker.
Manfred Rudolf Bihy 4-Dec-10 5:56am    
Neither am I.

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