2
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);
}

我可以使用此代码上传图片。但是,当我尝试连续添加相同的图像时,我再次收到写为标题的错误。我怎样才能解决这个问题?这是什么原因?

更新

图像管理器类

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;
    }
}

谢谢

4

2 回答 2

5

好吧,对我来说,它看起来像:
1.您正在创建图像Image smallImage = Image.FromFile(path);
2.您正在用新图像覆盖您的引用smallImage = ImageManager.ResizeImage(smallImage, size);
3.传递给方法ResizeImage的图像仍在内存中,因此您必须在返回新图像之前将其处理在该方法中b

imgToResize.Dispose();
return (Image)b;
于 2012-12-11T21:49:00.110 回答
3

此外,当您完成Image. 关闭它。我发现这是我的问题,因为我在上传后读取图像以进行一些处理。下一次上传我会得到一个锁定的文件。

Image smallImage = Image.FromFile(path);
//....
smallImage.Dispose();

此外,您应该将您的包装Graphics g在 using 语句中。它会在完成或出现异常时调用 Dispose。

using (Graphics g = Graphics.FromImage((Image)b)
{
    //....
}
//No need to call Dispose() manually.

甚至更好。确保您的图像在出现异常时被处理掉。调用块Image.Dispose()Finally

于 2013-02-28T05:42:04.490 回答