title: Resizing images in C#
body: <p>I have the below method which I found online,which resizes an image to an approximate size, while keeping the aspect ratio.</p>
  
  <pre><code>     public Image ResizeImage(Size size)
      {
          int sourceWidth = _Image.Width;
          int sourceHeight = _Image.Height;
  
          float nPercent = 0;
          float nPercentW = 0;
          float nPercentH = 0;
  
          nPercentW = ((float)size.Width / (float)sourceWidth);
          nPercentH = ((float)size.Height / (float)sourceHeight);
  
          if (nPercentH &gt; nPercentW)
              nPercent = nPercentH;
          else
              nPercent = nPercentW;
  
          int destWidth = (int)(sourceWidth * nPercent);
          int destHeight = (int)(sourceHeight * nPercent);
  
          Bitmap b = new Bitmap(destWidth, destHeight);
          Graphics g = Graphics.FromImage((Image)b);
          g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  
          g.DrawImage(_Image, 0, 0, destWidth, destHeight);
          g.Dispose();
  
          return (Image)b;
      }
  </code></pre>
  
  <p>I usually pass in a size with a width of 100px and a height of 100px - as part of my requirements I can't have any single dimension (height or width) being under 100px, so if the aspect ratio isn't square the other dimension would be higher.</p>
  
  <p>What I'm finding with this method is occasionally one of the dimensions will be under 100px - such as 96px or 99px. How can I change this method to ensure this doesn't happen?</p>