title: How to resize my image and maintain the aspect ratio?
body: <p>I'm trying to resize my picture so it fits the 640x640 size and keep the aspect ratio.</p>
  
  <p>For example, if this is the original picture: <a href="http://i.imgur.com/WEMCSyd.jpg" rel="nofollow">http://i.imgur.com/WEMCSyd.jpg</a>
  I want to resize in this way: <a href="http://i.imgur.com/K2BalOm.jpg" rel="nofollow">http://i.imgur.com/K2BalOm.jpg</a> to keep the aspect ratio (Basically, the image is always in the middle, and keeps the aspect ratio, the rest of space remains white)</p>
  
  <p>I have tried making a program in C# which has this code:</p>
  
  <pre><code>Bitmap originalImage, resizedImage;
  
              try
              {
                  using (FileStream fs = new FileStream(textBox1.Text, System.IO.FileMode.Open))
                  {
                      originalImage = new Bitmap(fs);
                  }
  
                  int imgHeight = 640;
                  int imgWidth = 640;
  
                  if (originalImage.Height == originalImage.Width)
                  {
                      resizedImage = new Bitmap(originalImage, imgHeight, imgWidth);
                  }
                  else
                  {
                      float aspect = originalImage.Width / (float)originalImage.Height;
                      int newHeight;
                      int newWidth;
  
                      newWidth = (int)(imgWidth / aspect);
                      newHeight = (int)(newWidth / aspect);
  
                      if (newWidth &gt; imgWidth || newHeight &gt; imgHeight)
                      {
                          if (newWidth &gt; newHeight)
                          {
                              newWidth = newHeight;
                              newHeight = (int)(newWidth / aspect);
                          }
                          else
                          {
                              newHeight = newWidth;
                              newWidth = (int)(newHeight / aspect);
                          }
                      }
  
                      resizedImage = new Bitmap(originalImage, newWidth, newHeight);
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
  </code></pre>
  
  <p>But it doesn't work the way I need it to.</p>