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?

Konamiman
  • 49,681
  • 17
  • 108
  • 138
shahroz
  • 359
  • 1
  • 6
  • 17

3 Answers3

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);
        }
mehmet mecek
  • 2,615
  • 2
  • 21
  • 25
  • Won't `b` be disposed at the end of the outer `using`? What's the advantage of calling `Dispose` directly? – mdm Oct 26 '15 at 08:58
  • its true.i forget to Dispose it.thanks. – shahroz Oct 26 '15 at 08:59
  • @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. – mehmet mecek Oct 26 '15 at 09:01
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

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

Spartan
  • 146
  • 1
  • 1
  • 7