目标参数:
float width = 1024;
float height = 768;
var brush = new SolidBrush(Color.Black);
您的原始文件:
var image = new Bitmap(file);
目标大小(比例因子):
float scale = Math.Min(width / image.Width, height / image.Height);
调整大小包括首先刷画布:
var bmp = new Bitmap((int)width, (int)height);
var graph = Graphics.FromImage(bmp);
// uncomment for higher quality output
//graph.InterpolationMode = InterpolationMode.High;
//graph.CompositingQuality = CompositingQuality.HighQuality;
//graph.SmoothingMode = SmoothingMode.AntiAlias;
var scaleWidth = (int)(image.Width * scale);
var scaleHeight = (int)(image.Height * scale);
graph.FillRectangle(brush, new RectangleF(0, 0, width, height));
graph.DrawImage(image, ((int)width - scaleWidth)/2, ((int)height - scaleHeight)/2, scaleWidth, scaleHeight);
并且不要忘记bmp.Save(filename)
保存生成的文件。