/// <summary> /// 根据指定的宽高,等比缩放图片,多余的部分会以白边补全 /// </summary> /// <param name="source"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="quality">质量,这个参数并不太会影响图像的呈现质量,主要是可以提高速度,质量越低速度越快</param> /// <param name="background"></param> /// <returns></returns> public static Bitmap Scale(this Image source, int width, int height, ImageQuality quality, Color background) { var rawRect = new DynamicRectangle() { X = 0, Y = 0, Width = source.Width, Height = source.Height }; var targetRect = rawRect.Scale(width, height, true); Bitmap target = null; try { target = new Bitmap(width, height); using (var gh = Graphics.FromImage(target)) { gh.CompositingQuality = quality.Compositing; gh.SmoothingMode = quality.Smoothing; gh.InterpolationMode = quality.Interpolation; gh.PixelOffsetMode = quality.PixelOffset; //gh.FillRectangle(new SolidBrush(background), 0, 0, width, height); //清空画布并以指定的背景色填充 gh.Clear(background); Rectangle destrect = targetRect.ToRectangle(); gh.DrawImage(source, destrect, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel); } } catch (Exception ex) { if (target != null) { target.Dispose(); } throw ex; } return(target); }
/// <summary> /// 将原始长宽缩放到指定的大小 /// 这时得到的是图像在指定区域中实际的偏移量和大小 /// </summary> /// <param name="targetWidth"></param> /// <param name="targetHeight"></param> /// <param name="isEnlarge">当源图片小于需要展现的区域大小时,是否需要将源图片放大</param> /// <returns></returns> public DynamicRectangle Scale(int targetWidth, int targetHeight, bool isEnlarge = false) { if (targetWidth < 0 || targetHeight < 0) { throw new DrawingException("DynamicRectangle 参数异常"); } if (targetWidth == 0 && targetHeight == 0) { return(this); } DynamicRectangle result = new DynamicRectangle(); result.X = 0; result.Y = 0; //先等比缩放 if (this.Width > this.Height) { //宽大于高,以宽等比缩放 if (isEnlarge) { result.Width = targetWidth; result.Height = (int)(this.Height * (double)result.Width / (double)this.Width); if (result.Height > targetHeight)//得到的结果比需要的高度高,则根据高度缩放 { result.Height = targetHeight; result.Width = (int)(this.Width * (double)result.Height / (double)this.Height); } } else { result.Width = this.Width > targetWidth ? targetWidth : this.Width; result.Height = (int)(this.Height * (double)result.Width / (double)this.Width); if (result.Height > targetHeight)//得到的结果比需要的高度高,则根据高度缩放 { result.Height = this.Height > targetHeight ? targetHeight : this.Height; result.Width = (int)(this.Width * (double)result.Height / (double)this.Height); } } } else { //高大于宽,以高等比缩放 if (isEnlarge) { result.Height = targetHeight; result.Width = (int)(this.Width * (double)result.Height / (double)this.Height); if (result.Width > targetWidth)//得到的结果比需要的宽度宽,则根据宽度缩放 { result.Width = targetWidth; result.Height = (int)(this.Height * (double)result.Width / (double)this.Width); } } else { result.Height = this.Height > targetHeight ? targetHeight : this.Height; result.Width = (int)(this.Width * (double)result.Height / (double)this.Height); if (result.Width > targetWidth)//得到的结果比需要的宽度宽,则根据宽度缩放 { result.Width = this.Width > targetWidth ? targetWidth : this.Width; result.Height = (int)(this.Height * (double)result.Width / (double)this.Width); } } } if (result.Width < targetWidth) { result.X = (targetWidth - result.Width) / 2; } if (result.Height < targetHeight) { result.Y = (targetHeight - result.Height) / 2; } return(result); }