我想同时保留从原始图像显示比例,使新的图像看起来不挤压调整图像大小。例如:将150 * 100的图像转换为150 * 150的图像。高度的额外50像素需要用白色背景色填充。这是我正在使用的当前代码。它可以很好地调整大小,但更改原始图像的纵横比会挤压新图像。private void resizeImage(string path, string originalFilename, int width, int height) { Image image = Image.FromFile(path + originalFilename); System.Drawing.Image thumbnail = new Bitmap(width, height); System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(thumbnail); graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; graphic.SmoothingMode = SmoothingMode.HighQuality; graphic.PixelOffsetMode = PixelOffsetMode.HighQuality; graphic.CompositingQuality = CompositingQuality.HighQuality; graphic.DrawImage(image, 0, 0, width, height); System.Drawing.Imaging.ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders(); EncoderParameters encoderParameters; encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L); thumbnail.Save(path + width + "." + originalFilename, info[1], encoderParameters); }编辑:我想有图像填充而不是裁剪
3 回答
动漫人物
TA贡献1815条经验 获得超10个赞
我使用以下方法来计算所需的图像尺寸:
using System.Drawing;
public static Size ResizeKeepAspect(this Size src, int maxWidth, int maxHeight, bool enlarge = false)
{
maxWidth = enlarge ? maxWidth : Math.Min(maxWidth, src.Width);
maxHeight = enlarge ? maxHeight : Math.Min(maxHeight, src.Height);
decimal rnd = Math.Min(maxWidth / (decimal)src.Width, maxHeight / (decimal)src.Height);
return new Size((int)Math.Round(src.Width * rnd), (int)Math.Round(src.Height * rnd));
}
这将纵横比和尺寸的问题放在单独的方法中。
- 3 回答
- 0 关注
- 350 浏览
添加回答
举报
0/150
提交
取消