public static void ImageScale(Bitmap src, Bitmap dest)
{
if (src == null || dest == null)
throw new ArgumentNullException();
double srcScale;
double destScale;
srcScale = (double)src.Width / src.Height;
destScale = (double)dest.Width / dest.Height;
using (Graphics g = Graphics.FromImage(dest))
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
if (srcScale - destScale >= 0 && srcScale - destScale <= 0.001)
{
g.DrawImage(src, new Rectangle(0, 0, dest.Width, dest.Height), new Rectangle(0, 0, src.Width, src.Height), GraphicsUnit.Pixel);
}
else if (srcScale < destScale)
{
double newHeight;
newHeight = (double)dest.Height * src.Width / dest.Width;
g.DrawImage(src, new Rectangle(0, 0, dest.Width, dest.Height), new Rectangle(0, (int)((src.Height - newHeight) / 2), src.Width, (int)newHeight), GraphicsUnit.Pixel);
}
else
{
double newWidth;
newWidth = (double)dest.Width * src.Height / dest.Height;
g.DrawImage(src, new Rectangle(0, 0, dest.Width, dest.Height), new Rectangle((int)((src.Width - newWidth) / 2), 0, (int)newWidth, src.Height), GraphicsUnit.Pixel);
}
}