C#图片缩放,利用插值去除缩图毛边。
/// <summary>
/// 缩放图片
/// </summary>
/// <param name="original">原始图片</param>
/// <param name="width">新的宽度</param>
/// <param name="height">新的高度</param>
/// <returns>处理以后的图片</returns>
public static Bitmap ResizeImage(Bitmap original, int width, int height) {
try {
// 生成新画布
Bitmap image = new Bitmap(width, height);
// 获取GDI+绘图图画
Graphics graph = Graphics.FromImage(image);
// 插值算法的质量
graph.CompositingQuality = CompositingQuality.HighQuality;
graph.SmoothingMode = SmoothingMode.HighQuality;
graph.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 缩放图片
graph.DrawImage(original, new Rectangle(0, 0, width, height),
new Rectangle(0, 0, original.Width, original.Height), GraphicsUnit.Pixel);
// 保存绘制结果
graph.Save();
// 释放画笔内存
graph.Dispose();
// 返回缩放图片
return image;
} catch {
return null;
}
}
C#图片缩放并补白。
/// <summary>
/// 缩放并补白图片
/// </summary>
/// <param name="original">原始图片</param>
/// <param name="width">新的宽度</param>
/// <param name="height">新的高度</param>
/// <param name="margin">补白宽度</param>
/// <returns>处理以后的图片</returns>
public static Bitmap FillerImage(Bitmap original, int width, int height, int margin) {
try {
// 生成新画布
Bitmap image = new Bitmap(original.Width + 2 * margin, original.Height + 2 * margin);
// 获取GDI+绘图图画
Graphics graph = Graphics.FromImage(image);
// 定义画笔
SolidBrush brush = new SolidBrush(Color.White);
// 绘制背景色
graph.FillRectangle(brush, new Rectangle(0, 0, image.Width, image.Height));
// 叠加图片
graph.DrawImageUnscaled(original, margin, margin);
// 保存绘制结果
graph.Save();
// 释放GDI+绘图图画内存
graph.Dispose();
// 释放画笔内存
brush.Dispose();
// 缩放并返回处理后的图片
return ResizeImage(image, width, height);
} catch {
return null;
}
}