/// <summary> /// 生成缩略图并打水印再保存。 /// </summary> /// <Param name="originalImageStream">Stream</Param> /// <Param name="strFileName">源图路径(物理路径)</Param> /// <Param name="thumbnailPath">缩略图路径(物理路径)</Param> /// <Param name="maxWidth">缩略图最大宽度</Param> /// <Param name="maxheight">缩略图最大高度</Param> /// <Param name="mode">生成缩略图的方式</Param> /// <Param name="waterPath">水印图路径(物理路径)</Param> /// <Param name="edge">水印图离原图边界的距离</Param> /// <Param name="position">加图片水印的位置</Param> /// <returns>是否成功</returns> public static bool MakeThumbnailWater(Stream originalImageStream, string strFileName, string thumbnailPath, int maxWidth, int maxheight, ThumbnailModeOption mode, string waterPath, int edge, WaterPositionOption position) { bool success = false; try { // 生成缩略图 MakeThumbnail(originalImageStream, strFileName, thumbnailPath, maxWidth, maxheight, mode); Stream stream = File.Open(thumbnailPath, FileMode.Open); //打水印 MakeWaterImage(stream, strFileName, thumbnailPath, waterPath, edge, position); success = true; } catch (Exception /*ex*/) { success = false; throw; } return(success); }
/// <summary> /// 生成缩略图并打水印再保存。 /// </summary> /// <param name="originalImageStream">Stream</param> /// <param name="strFileName">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="maxWidth">缩略图最大宽度</param> /// <param name="maxheight">缩略图最大高度</param> /// <param name="mode">生成缩略图的方式</param> /// <param name="waterPath">水印图路径(物理路径)</param> /// <param name="edge">水印图离原图边界的距离</param> /// <param name="position">加图片水印的位置</param> /// <returns>是否成功</returns> public static bool MakeThumbnailWater(Stream originalImageStream, string strFileName, string thumbnailPath, int maxWidth, int maxheight, ThumbnailModeOption mode, string waterPath, int edge, WaterPositionOption position) { bool success = false; try { // 生成缩略图 MakeThumbnail(originalImageStream, strFileName, thumbnailPath, maxWidth, maxheight, mode); Stream stream = File.Open(thumbnailPath, FileMode.Open); //打水印 MakeWaterImage(stream, strFileName, thumbnailPath, waterPath, edge, position); success = true; } catch (Exception ex) { success = false; throw; } return success; }
/// <summary> /// 生成缩略图并保存。 /// </summary> /// <Param name="originalImageStream">Stream</Param> /// <Param name="strFileName">源图路径(物理路径)</Param> /// <Param name="thumbnailPath">缩略图路径(物理路径)</Param> /// <Param name="maxWidth">缩略图最大宽度</Param> /// <Param name="maxheight">缩略图最大高度</Param> /// <Param name="mode">生成缩略图的方式</Param> /// <returns>是否成功</returns> public static bool MakeThumbnail(Stream originalImageStream, string strFileName, string thumbnailPath, int maxWidth, int maxheight, ThumbnailModeOption mode) { bool success = false; int x = 0; int y = 0; int toW = maxWidth; int toH = maxheight; Image image = null; Image bitmap = null; Graphics graphics = null; try { image = Image.FromStream(originalImageStream); int w = image.Width; int h = image.Height; switch (mode) { case ThumbnailModeOption.WH: break; case ThumbnailModeOption.W: if (w < maxWidth) { toW = w; toH = h; } else { toH = h * maxWidth / w; } break; case ThumbnailModeOption.H: if (h < maxheight) { toW = w; toH = h; } else { toW = w * maxheight / h; } break; case ThumbnailModeOption.CUT: if (((double)w / (double)h) > ((double)toW / (double)toH)) { w = h * toW / toH; y = 0; x = (image.Width - w) / 2; } else { h = w * toH / toW; x = 0; y = (image.Height - h) / 2; } break; default: goto case ThumbnailModeOption.CUT; } bitmap = new Bitmap(toW, toH); graphics = Graphics.FromImage(bitmap); graphics.InterpolationMode = InterpolationMode.High; //设置高质量,低速度呈现平滑程度 graphics.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 graphics.Clear(Color.Transparent); // 在指定位置并且按指定大小绘制原图片的指定部分 graphics.DrawImage(image, new Rectangle(0, 0, toW, toH), new Rectangle(x, y, w, h), GraphicsUnit.Pixel); // 保存缩略图 bitmap.Save(thumbnailPath, GetImageFormat(strFileName)); success = true; } catch (Exception /*ex*/) { success = false; throw; } finally { if (graphics != null) { graphics.Dispose(); } if (bitmap != null) { bitmap.Dispose(); } if (image != null) { image.Dispose(); } } return(success); }
/// <summary> /// 生成缩略图并保存。 /// </summary> /// <param name="originalImageStream">Stream</param> /// <param name="strFileName">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="maxWidth">缩略图最大宽度</param> /// <param name="maxheight">缩略图最大高度</param> /// <param name="mode">生成缩略图的方式</param> /// <returns>是否成功</returns> public static bool MakeThumbnail(Stream originalImageStream, string strFileName, string thumbnailPath, int maxWidth, int maxheight, ThumbnailModeOption mode) { bool success = false; int x = 0; int y = 0; int toW = maxWidth; int toH = maxheight; Image image = null; Image bitmap = null; Graphics graphics = null; try { image = Image.FromStream(originalImageStream); int w = image.Width; int h = image.Height; switch (mode) { case ThumbnailModeOption.WH: break; case ThumbnailModeOption.W: if (w < maxWidth) { toW = w; toH = h; } else { toH = h * maxWidth / w; } break; case ThumbnailModeOption.H: if (h < maxheight) { toW = w; toH = h; } else { toW = w * maxheight / h; } break; case ThumbnailModeOption.CUT: if (((double)w / (double)h) > ((double)toW / (double)toH)) { w = h * toW / toH; y = 0; x = (image.Width - w) / 2; } else { h = w * toH / toW; x = 0; y = (image.Height - h) / 2; } break; default: goto case ThumbnailModeOption.CUT; } bitmap = new Bitmap(toW, toH); graphics = Graphics.FromImage(bitmap); graphics.InterpolationMode = InterpolationMode.High; //设置高质量,低速度呈现平滑程度 graphics.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 graphics.Clear(Color.Transparent); // 在指定位置并且按指定大小绘制原图片的指定部分 graphics.DrawImage(image, new Rectangle(0, 0, toW, toH), new Rectangle(x, y, w, h), GraphicsUnit.Pixel); // 保存缩略图 bitmap.Save(thumbnailPath, GetImageFormat(strFileName)); success = true; } catch (Exception ex) { success = false; throw; } finally { if (graphics != null) graphics.Dispose(); if (bitmap != null) bitmap.Dispose(); if (image != null) image.Dispose(); } return success; }