/// <summary> /// 保存缩略图 /// </summary> /// <param name="savePath">保存路径</param> /// <param name="thumbWidth">缩略图限制宽度</param> /// <param name="thumbHeight">缩略图限制高度(为null时 表示不限制高度)</param> public void Save(string savePath, int thumbWidth, int?thumbHeight) { using (Image originalImg = Image.FromStream(_stream)) { Size size = originalImg.Size; switch (_zoomMode) { case ZoomMode.GeometricProportion: if (size.Width > thumbWidth || size.Height > thumbHeight) { float scale; if (thumbHeight.HasValue) { scale = Math.Min((float)thumbWidth / size.Width, (float)thumbHeight / size.Height); } else { scale = (float)thumbWidth / size.Width; } size = new Size((int)(scale * size.Width), (int)(scale * size.Height)); } break; case ZoomMode.Fixed: if (thumbHeight.HasValue == false) { throw new ArgumentNullException("thumbHeight", "固定缩放模式下必须指定高度"); } size = new Size(thumbWidth, thumbHeight.Value); break; } using (Bitmap bmp = new Bitmap(size.Width, size.Height)) using (Graphics g = Graphics.FromImage(bmp)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.Transparent); g.DrawImage(originalImg, 0, 0, size.Width, size.Height); FileUtils.AutoCreateDir(savePath); bmp.Save(savePath, ImageFormat.Jpeg); } } }