20

i have developed a web application using asp.net mvc4 and razor. in my application there's a file upload control to upload an image and save in a temporary location.

before save image should re-sized to a specific size and then save in the temporary location given.

here is the code i have used in controller class.

public class FileUploadController : Controller
{
    //
    // GET: /FileUpload/

    public ActionResult Index()
    {
        return View();
    }
    public ActionResult FileUpload()
    {
        return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult FileUpload(HttpPostedFileBase uploadFile)
    {
        if (uploadFile.ContentLength > 0)
        {
            string relativePath = "~/img/" + Path.GetFileName(uploadFile.FileName);
            string physicalPath = Server.MapPath(relativePath);


            FileUploadModel.ResizeAndSave(relativePath, uploadFile.FileName, uploadFile.InputStream, uploadFile.ContentLength, true);

            return View((object)relativePath);
        }
        return View();
    }
}

and here is the code used in model class

public class FileUploadModel
{
    [Required]
    public HttpPostedFileWrapper ImageUploaded { get; set; }

    public static void ResizeAndSave(string savePath, string fileName, Stream imageBuffer, int maxSideSize, bool makeItSquare)
    {
        int newWidth;
        int newHeight;
        Image image = Image.FromStream(imageBuffer);
        int oldWidth = image.Width;
        int oldHeight = image.Height;
        Bitmap newImage;
        if (makeItSquare)
        {
            int smallerSide = oldWidth >= oldHeight ? oldHeight : oldWidth;
            double coeficient = maxSideSize / (double)smallerSide;
            newWidth = Convert.ToInt32(coeficient * oldWidth);
            newHeight = Convert.ToInt32(coeficient * oldHeight);
            Bitmap tempImage = new Bitmap(image, newWidth, newHeight);
            int cropX = (newWidth - maxSideSize) / 2;
            int cropY = (newHeight - maxSideSize) / 2;
            newImage = new Bitmap(maxSideSize, maxSideSize);
            Graphics tempGraphic = Graphics.FromImage(newImage);
            tempGraphic.SmoothingMode = SmoothingMode.AntiAlias;
            tempGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            tempGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
            tempGraphic.DrawImage(tempImage, new Rectangle(0, 0, maxSideSize, maxSideSize), cropX, cropY, maxSideSize, maxSideSize, GraphicsUnit.Pixel);
        }
        else
        {
            int maxSide = oldWidth >= oldHeight ? oldWidth : oldHeight;

            if (maxSide > maxSideSize)
            {
                double coeficient = maxSideSize / (double)maxSide;
                newWidth = Convert.ToInt32(coeficient * oldWidth);
                newHeight = Convert.ToInt32(coeficient * oldHeight);
            }
            else
            {
                newWidth = oldWidth;
                newHeight = oldHeight;
            }
            newImage = new Bitmap(image, newWidth, newHeight);
        }
        newImage.Save(savePath + fileName + ".jpg", ImageFormat.Jpeg);
        image.Dispose();
        newImage.Dispose();
    }
}

but when i run the application it occurs an ArgumentException.

it says "Parameter is not valid" in following code line

Bitmap tempImage = new Bitmap(image, newWidth, newHeight);

how do i pass valid and appropriate parameters here

public static void ResizeAndSave(string savePath, string fileName, Stream imageBuffer, int maxSideSize, bool makeItSquare)
sanzy
  • 805
  • 9
  • 18
  • 28

1 Answers1

71

Its very difficult to understand what is the problem with your code. But may be you want to use alternative way. You need to add the reference to System.Web.Helpers namespace and try the following code.

[HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        WebImage img = new WebImage(file.InputStream);
        if (img.Width > 1000)
            img.Resize(1000, 1000);
        img.Save("path");
        return View();
    }

Also this class supports the crop, flip, watermark operation etc.

maxs87
  • 2,274
  • 2
  • 19
  • 23
  • Hi.. thanks for uploading the post.but your code is working if the image sizes is under 1 MB..if i upload 3 MB or 2 MB image size than it give error message "Out of memory."Can u please help me... – user3217843 Oct 25 '16 at 06:32
  • Thanks! Just what I was looking for. – Nitin Badole Nov 21 '16 at 10:52
  • Hi.. On local machine its working fine but whenever i deploy it its throwing webpages ka exception. – user3217843 Dec 13 '16 at 07:45
  • @user3217843 your Problem can be solved by increasing Runtime. Because it takes little longer for the system to Upload 2MB File in order to solve this go to your Web.Config Under Write '' What this will do is it will Increase the time of the system and your Memory Problem will be solved – Abdul Hannan Dec 21 '16 at 15:57
  • Abdul Hannan Have already increased the limit in config but no use..was getting the same issue.. – user3217843 Jan 23 '17 at 05:32
  • my issue is fixed by adding the bindingConfiguration of the service. – user3217843 Jun 30 '17 at 09:47
  • There is no need to If statement : img.Resize(1000, 1, true, true); – osmanraifgunes Aug 28 '18 at 21:52
  • I was going everywhere on the net to find this and here it is. Thank you so much! – Nizar Jul 15 '20 at 13:16