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.
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.