title: C# : How to resize image proportionately with max height
body: <p>I need to resize my image proportionately without changing aspect ratio.I have the code to resize with fixed hight and width but I need to resize image proportionately with max height(say 600 pixels). How can I modify the code to suit my requirement?</p>
  
  <pre><code>public static void Main()
  {
    var image = Image.FromFile(@"c:\logo.png");
    var newImage = ScaleImage(image, 300, 400);
    newImage.Save(@"c:\test.png", ImageFormat.Png);
  }
  
  public static Image ScaleImage(Image image, int maxWidth, int maxHeight)
   {
    var ratioX = (double)maxWidth / image.Width;
    var ratioY = (double)maxHeight / image.Height;
    var ratio = Math.Min(ratioX, ratioY);
  
    var newWidth = (int)(image.Width * ratio);
    var newHeight = (int)(image.Height * ratio);
  
    var newImage = new Bitmap(newWidth, newHeight);
    Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
    return newImage;
  }
  </code></pre>
  
  <p>Please provide your valuable thoughts.</p>