/// <summary> /// 从输入的Image对象,用指定的预置生成缩略图。 /// </summary> /// <param name="Input">要生成缩略图的Image对象。</param> /// <param name="Preset">有关缩略图生成过程参数的预置。</param> /// <param name="OutputStream">生成的缩略图输出到的流。</param> /// <returns>返回一个用指定编码器保存的图像的流。</returns> public static void RenderThumbnail(Image Input, ThumbnailPreset Preset, Stream OutputStream) { Rectangle CopyRectangle; Size RanderContainerSize; Rectangle RenderRectangle; CalculateSize(Input.Size, Preset.ThumbnailSize, Preset.ThumbnailResizeMode, out CopyRectangle, out RanderContainerSize, out RenderRectangle); using (Bitmap outbmp = new Bitmap(RanderContainerSize.Width, RanderContainerSize.Height)) { using (Graphics g = Graphics.FromImage(outbmp)) { //渲染背景色 g.DrawRectangle(new Pen(Preset.BackgroundColor), new Rectangle(new Point(0, 0), RanderContainerSize)); //渲染缩略图 g.DrawImage(Input, RenderRectangle, CopyRectangle, GraphicsUnit.Pixel); //还应根据预置渲染水印图案。 } //创建输出流。 ImageCodecInfo Encoder = GetEncoder(Preset.OutputFormat); if (Encoder == null) { throw new InvalidOperationException("操作无效。无法找到对应格式的编码器。"); } EncoderParameters encodeParams = new EncoderParameters(Preset.EncoderParams.Count); for (int i = 0; i < Preset.EncoderParams.Count; i++) { encodeParams.Param[i] = Preset.EncoderParams[i]; } outbmp.Save(OutputStream, Encoder, encodeParams); } }
/// <summary> /// 从输入的流用指定的预置生成缩略图。 /// </summary> /// <param name="Input">要生成缩略图的图像的流。</param> /// <param name="Preset">有关缩略图生成过程参数的预置。</param> /// <param name="OutputStream"></param> /// <returns>返回一个用指定编码器保存的图像的流。</returns> /// <exception cref="ArgumentException">流没有包含有效的图像。</exception> public static void RenderThumbnail(Stream Input, ThumbnailPreset Preset, Stream OutputStream) { Image img = Image.FromStream(Input, true, true); RenderThumbnail(img, Preset, OutputStream); }