Хотя я считаю ответ Джона Бокера достаточным, это может быть более краткий способ сделать это:
static Bitmap ResizeIsotropic(Image img, Size newSize, Color backgroundColor)
{
int Width, Height;
if (img.Width >= img.Height)
{
Width = newSize.Width;
Height = (int)(img.Height / (img.Width / Width));
}
else
{
Height = newSize.Height;
Width = (int)(img.Width / (img.Height / Height));
}
Bitmap bmp = new Bitmap(newSize.Width, newSize.Height);
using (Graphics gfx = Graphics.FromImage(bmp))
{
gfx.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gfx.Clear(backgroundColor);
gfx.DrawImage(img, (bmp.Width / 2) - (Width / 2), (bmp.Height / 2) - (Height / 2), Width, Height);
}
return bmp;
}
Затем, чтобы позвонить, просто сделайте:
Bitmap bmp = ResizeIsotropic(Image.FromFile(@"c:\yourImage.jpg"), new Size(512, 512), Color.Black);
bmp.Save(@"c:\test.bmp");