Reputation: 15866
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
Upvotes: 2
Views: 2919
Reputation: 24729
Also, when you are finished with the Image
. Close it. I found this was my problem as I was reading the image after upload to do some processing. The next upload I would get a locked file.
Image smallImage = Image.FromFile(path);
//....
smallImage.Dispose();
Also, you should wrap your Graphics g
in a using statement. It will call Dispose when finished or if there is an exception.
using (Graphics g = Graphics.FromImage((Image)b)
{
//....
}
//No need to call Dispose() manually.
Even better. Ensure your image is disposed of incase of exception. Call Image.Dispose()
in the Finally
block.
Upvotes: 3
Reputation: 3154
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;
Upvotes: 5