2

I edit pic file in path and create new image file for there.my code is:

string[] files = Directory.GetFiles(string.Concat(Server.MapPath("/"), "tmp/")); foreach (string path in files) { string filename = Path.GetFileName(path); using (Bitmap b = new Bitmap(string.Concat(Server.MapPath("/"), "tmp/", filename))) { SolidBrush pixelBrush = new SolidBrush(Color.White); Graphics g = Graphics.FromImage(b); g.FillRectangle(Brushes.White, 0, 0, 105, 40); string outputFileName = string.Concat(Server.MapPath("/"), "tmp\\E", filename); MemoryStream memory = new MemoryStream(); FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite); b.Save(memory, ImageFormat.Jpeg); byte[] bytes = memory.ToArray(); fs.Write(bytes, 0, bytes.Length); fs.Close(); memory.Close(); b.Dispose(); } File.Delete(path); } 

when delete old file error happend is:

Additional information: The process cannot access the file 'G:\project\WebApplication1\WebApplication1\tmp\b381ae6.jpg' because it is being used by another process.

how to fix it?

6
  • I use Using(...) , error happened Commented Oct 26, 2015 at 8:15
  • @GuruprasadRao The OP used using, so the file stream is supposed to close automatically as in the answer said, so I don't think that post helps much.. Commented Oct 26, 2015 at 8:20
  • 1
    Give the folder(where the files are in) access to everyone (in sharing).. and try to list filename you will delete and delete them outside foreach loop Commented Oct 26, 2015 at 8:26
  • Try keeping all the stuffs in one using and check you are still getting error? Commented Oct 26, 2015 at 8:27
  • ok.i edit code.error happened. Commented Oct 26, 2015 at 8:29

3 Answers 3

1

Wrapping Graphics with using will fix it. You should dispose it also.

 using (Bitmap b = new Bitmap(filePath)) { using (Graphics g = Graphics.FromImage(b)) { ... } } 

Also you can use using statements by combining them.

 foreach (var path in files) { var filename = Path.GetFileName(path); var filePath = string.Concat(tmpPath, filename); using (var b = new Bitmap(filePath)) using (var g = Graphics.FromImage(b)) { g.FillRectangle(Brushes.White, 0, 0, 105, 40); var outputFileName = string.Concat(newPath, filename); using (var memory = new MemoryStream()) using (var fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite)) { b.Save(memory, ImageFormat.Jpeg); var bytes = memory.ToArray(); fs.Write(bytes, 0, bytes.Length); } } File.Delete(path); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Won't b be disposed at the end of the outer using? What's the advantage of calling Dispose directly?
its true.i forget to Dispose it.thanks.
@mdm, I don't prefer using Dispose myself, instead use using statement. What I m sayin is the Graphics object should be disposed so wrapping it with using statement will fix the issue.
0

write code in using scope and check File exists

Example

 using (var b = new Bitmap(filePath)) using (var g = Graphics.FromImage(b)) using (var memory = new MemoryStream()) using (var fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite)) bool exists = File.Exists("Path"); if(exists) { // Code Here } 

1 Comment

Files is Exists but used by another process.
-1

You need to close the filestream before doing other operations on that file ;)

4 Comments

the OP used "using" already, so the filstream supposed to close automatically;
i close it.but error .....
i think error is on Directory.GetFiles()!?
then maybe the problem occurs because he is doing the delete inside the foreach instruction

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.