title: How to crop or resize Image to set aspect ratio in asp.net
body: <p>I have an image control in my webform. I set its width 100px and height 100px. but if someone upload an image of ratio 100 * 120. I want it crop or resize and set 100 * 100. I tried to set max width but not worked , I tried bitmap method with code </p>
  
  <pre><code> protected void Button1_Click(object sender, EventArgs e)
  {
      if (FileUpload1.HasFile)
      {
          string filename = FileUpload1.FileName;
          string directory = Server.MapPath("~/");
          Bitmap originalBMP = new Bitmap(FileUpload1.FileContent);
          float origWidth = originalBMP.Width;
          float origHeight = originalBMP.Height;
          float sngRatio = origWidth / origHeight;
          float newWidth = 100;
          float newHeight = newWidth / sngRatio;
          Bitmap newBMP = new Bitmap(originalBMP,Convert.ToInt32(newWidth),Convert.ToInt32(newHeight));
          Graphics oGraphics = Graphics.FromImage(newBMP);
          oGraphics.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
          oGraphics.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
          oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
          newBMP.Save(directory + filename);
          originalBMP = null;
          newBMP = null;
          oGraphics = null;
          Label1.Text = "File &lt;b style='color: red;'&gt;" + filename + "&amp;lt;&lt;/b&gt; uploaded.";
          Image1.ImageUrl = @"~/" + filename;
      }
      else
      {
          Label1.Text = "No file uploaded!";
      }
  }
  </code></pre>
  
  <p>it worked but it save the resized image in directery I want to save original image in directory and display the resize image in image control.</p>