title: How to resize and save an image which uploaded using file upload control in c#
body: <p>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.</p>
  
  <p>before save image should re-sized to a specific size and then save in the temporary location given.</p>
  
  <p>here is the code i have used in controller class.</p>
  
  <pre><code>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 &gt; 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();
      }
  }
  </code></pre>
  
  <p>and here is the code used in model class</p>
  
  <pre><code>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 &gt;= 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 &gt;= oldHeight ? oldWidth : oldHeight;
  
              if (maxSide &gt; 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();
      }
  }
  </code></pre>
  
  <p>but when i run the application it occurs an <strong>ArgumentException</strong>.</p>
  
  <p>it says <strong>"Parameter is not valid"</strong> in following code line</p>
  
  <pre><code>Bitmap tempImage = new Bitmap(image, newWidth, newHeight);
  </code></pre>
  
  <p>how do i pass valid and appropriate parameters here</p>
  
  <pre><code>public static void ResizeAndSave(string savePath, string fileName, Stream imageBuffer, int maxSideSize, bool makeItSquare)
  </code></pre>