-1

What do I need to do to end imagePath process?

Error:

The process cannot access the file 'C:\Users\Rafal\7074edcf-8849-4ea7-a87d-e2e8b5890f3f.jpg' because it is being used by another process.

enter image description here

public WrappedJsonResult2 UploadImageSmall(HttpPostedFileWrapper imageFile2)
        {

            if (imageFile2 == null || imageFile2.ContentLength == 0)
            {
                return new WrappedJsonResult2
                {
                    Data = new
                    {
                        IsValid = false,
                        Message = "Nie dodano zdjęcia",
                        ImagePath = string.Empty
                    }
                };
            }

here I have fileName and imagePath for first picture

            var fileName = String.Format("{0}.jpg", Guid.NewGuid().ToString());
            var imagePath = Path.Combine(Server.MapPath(Url.Content("~/Content/UserImages")), fileName);

here I have fileName and imagePath for second picture

            var fileNameZmniejszony = String.Format("{0}.jpg", Guid.NewGuid().ToString());
            var imagePathZmniejszony = Path.Combine(Server.MapPath(Url.Content("~/Content/UserImages")), fileNameZmniejszony);

I save picture from JSON

            imageFile2.SaveAs(imagePath);

I take picture from direction of imageFile2

            var image = Image.FromFile(imagePath);

I scale new picture and save as second Image

            var newImage = ScaleImage(image, 300, 400);
            newImage.Save(imagePathZmniejszony);

Here I would like to delete file with directory first image

            if (System.IO.File.Exists(imagePath))
            {
                System.IO.File.Delete(imagePath);
            }

            var model = new StronaGlowna();
            if (!TryUpdateModel(model))
            {
            }
            model.MaleZdjecie = String.Format("~/Content/UserImages/{0}", fileNameZmniejszony);

            return new WrappedJsonResult2
            {
                Data = new
                {
                    IsValid = true,
                    Message = string.Empty,
                    ImagePath = Url.Content(String.Format("~/Content/UserImages/{0}", fileNameZmniejszony))
                }
            };
        }

Why I have got problem with process because this take process (HttpPostedFileWrapper imageFile2)

and dispose() will not work for this problem.

Rafał Developer
  • 2,135
  • 9
  • 40
  • 72
  • Duplicate of [The process cannot access the file because it is being used by another process](http://stackoverflow.com/questions/877889/the-process-cannot-access-the-file-because-it-is-being-used-by-another-process) – CodeCaster Mar 26 '13 at 15:25

3 Answers3

7

You need to Dispose() your image before you'll be able to delete it. Right now, you have the image file opened.

This is documented in Image.FromFile

The file remains locked until the Image is disposed.

In your code, one way to handle this is to write:

using(var image = Image.FromFile(imagePath))
{
   using(var newImage = ScaleImage(image, 300, 400))
   {
       newImage.Save(imagePathZmniejszony);
   }
}

This will cause both Image instances to be Disposed of properly.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
2

Try using image.Dispose() before deleting it.

Felix
  • 1,006
  • 8
  • 22
2

You need to dispose your image variable. It is still using the file.

call image.Dispose(); before doing the delete.

You can also wrap your image variable in an using statement when you go to copy it. This is better practice.

using (var image = Image.FromFile(imagePath))
{
    var newImage = ScaleImage(image, 300, 400);
    newImage.Save(imagePathZmniejszony);
}

The above will fix the issue. Always wrap disposable objects in an Using statement. That clears all unmanaged resources from memory.

Caleb Keith
  • 816
  • 4
  • 10