title: c# Image resizing to different size while preserving aspect ratio
body: <p>I'm trying to resize an image while preserving the aspect ratio from the original image so the new image doesn't look squashed.</p>
  
  <p>eg:</p>
  
  <blockquote>
    <p>Convert a 150*100 image into a 150*150 image.<br>
    The extra 50 pixels of the height need to be padded with a white background color.</p>
  </blockquote>
  
  <p>This is the current code I am using. </p>
  
  <p>It works well for resizing but changing the aspect ratio of the original image squashes the new image.</p>
  
  <pre><code>private void resizeImage(string path, string originalFilename, 
                           int width, int height)
      {
          Image image = Image.FromFile(path + originalFilename);
  
          System.Drawing.Image thumbnail = new Bitmap(width, height);
          System.Drawing.Graphics graphic = 
                       System.Drawing.Graphics.FromImage(thumbnail);
  
          graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
          graphic.SmoothingMode = SmoothingMode.HighQuality;
          graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
          graphic.CompositingQuality = CompositingQuality.HighQuality;
  
          graphic.DrawImage(image, 0, 0, width, height);
  
          System.Drawing.Imaging.ImageCodecInfo[] info =
                           ImageCodecInfo.GetImageEncoders();
          EncoderParameters encoderParameters;
          encoderParameters = new EncoderParameters(1);
          encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,
                           100L);            
          thumbnail.Save(path + width + "." + originalFilename, info[1], 
                           encoderParameters);
      }
  </code></pre>
  
  <p>EDIT: I'd like to have the image padded instead of cropped</p>