Search code examples
asp.net-mvcimagefile-uploadprocessdispose

the process cannot access the file because it is being used by another process?


public ActionResult _Upload(HttpPostedFileBase file, GaleriesViewModel galeriesViewModel)
{
    Images image = new Images();

    if (file.ContentLength > 0)
    {
        try
        {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("~/Images/Galeries/Galery_" + galeriesViewModel.sno), fileName);
            var smallImagePath = Path.Combine(Server.MapPath("~/Images/Galeries/Small/Galery_" + galeriesViewModel.sno), fileName);
            var db_file_url = "Images/Galeries/Galery_" + galeriesViewModel.sno + "/" + fileName;
            var db_small_image_url = "Images/Galeries/Small/Galery_" + galeriesViewModel.sno + "/" + fileName;

            //exception thrown in this line
            file.SaveAs(path);
            Image smallImage = Image.FromFile(path);
            Size size = new Size();
            size.Height = 128;
            size.Width = 128;
            smallImage = ImageManager.ResizeImage(smallImage, size);
            smallImage.Save(smallImagePath);
            smallImage.Dispose();

            image.ContentType = file.ContentType;
            image.CreatedOn = DateTime.Now;
            //image.CreateUserId = WebSecurity.CurrentUserId;
            image.GaleryId = galeriesViewModel.sno;
            image.ImageUrl = db_file_url;
            image.Name = fileName;
            image.UploadSize = file.ContentLength;
            image.SmallImageUrl = db_small_image_url;
            entity.Images.Add(image);
            entity.SaveChanges();
        }
        catch (Exception ex) { }
    }



    galeriesViewModel.Galeries = entity.Galeries;
    ViewData["Selected"] = galeriesViewModel.sno;
    return View("ImageOperations", galeriesViewModel);
}

I can upload image with this code. But, When I try to add same image consecutivly, again, I get error that is written as title. How can I fix this? What is the reason of this?

UPDATE

imageManager Class

public static class ImageManager
{
    public static Image ResizeImage(Image imgToResize, Size size)
    {
        int sourceWidth = imgToResize.Width;
        int sourceHeight = imgToResize.Height;

        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;

        nPercentW = ((float)size.Width / (float)sourceWidth);
        nPercentH = ((float)size.Height / (float)sourceHeight);

        if (nPercentH < nPercentW)
            nPercent = nPercentH;
        else
            nPercent = nPercentW;

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap b = new Bitmap(destWidth, destHeight);
        Graphics g = Graphics.FromImage((Image)b);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
        g.Dispose();

        return (Image)b;
    }
}

Thanks


Solution

  • Ok for me it looks like:
    1.You are creating image Image smallImage = Image.FromFile(path);
    2.You are overriding your reference by new image smallImage = ImageManager.ResizeImage(smallImage, size);
    3.Your image passed to method ResizeImage is still in memory so you have to dispose it in that method before returning new image b

    imgToResize.Dispose();
    return (Image)b;