/// <summary> /// 生成缩略图 /// </summary> public string ToThumbnailImage() { if (this.SourceImagePath.ToString() == System.String.Empty) { throw new NullReferenceException("SourceImagePath is null!"); } string sExt = SourceImagePath.Substring(SourceImagePath.LastIndexOf(".")).ToLower(); if (!ExtensionValidator(sExt, 0)) { throw new ArgumentException("原图片文件格式不正确,支持的格式有[ " + MimeDict.ExpandAndToString(",") + " ]", "SourceImagePath"); } //从 原图片 创建 Image 对象 Image image = Image.FromFile(HttpContext.Current.Server.MapPath("~/" + this.SourceImagePath), false); Bitmap bitmap = new Bitmap(this.ThumbnailImageWidth, this.ThumbnailImageHeight); Graphics graphics = Graphics.FromImage(bitmap); //创建画板并加载空白图像 graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //设置保真模式为高度保真 graphics.DrawImage(image, new Rectangle(0, 0, this.ThumbnailImageWidth, this.ThumbnailImageHeight), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); //开始画图 image.Dispose(); try { //将此 原图片 以指定格式并用指定的编解码参数保存到指定文件 string savepath = (ThumbnailImagePath == null ? SourceImagePath : ThumbnailImagePath); Random rd = new Random(); string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + sExt; //新文件名 string path = HttpContext.Current.Server.MapPath("~/" + savepath); //保存路径 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } bitmap.Save(string.Concat(path, "\\", fileName)); return(savepath + "/" + fileName); } catch (System.Exception e) { throw e; } finally { bitmap.Dispose(); graphics.Dispose(); } }
/// <summary> /// 生成水印图片 /// </summary> /// <returns></returns> public string ToWaterMark(string FileName, int wm_x, int wm_y) { #region 创建Image对象 string savepath = (SaveWaterMarkImagePath == null ? SourceImagePath : SaveWaterMarkImagePath); string sExt = SourceImagePath.Substring(SourceImagePath.LastIndexOf(".")).ToLower(); if (this.SourceImagePath.ToString() == System.String.Empty) { throw new NullReferenceException("SourceImagePath is null!"); } if (!ExtensionValidator(sExt, 0)) { throw new ArgumentException("原图片文件格式不正确,支持的格式有[ " + MimeDict.Keys.ExpandAndToString(",") + " ]", "SourceImagePath"); } //从 原图片 创建 Image 对象 System.IO.FileStream fs = System.IO.File.OpenRead(HttpContext.Current.Server.MapPath("~/" + this.SourceImagePath)); Image s_image = Image.FromStream(fs, true); fs.Close(); int s_imagewidth = s_image.Width; int s_imageheight = s_image.Height; float s_imageHorizontalResolution = s_image.HorizontalResolution; float s_imageVerticalResolution = s_image.VerticalResolution; //指定的现有图像并使用指定的大小初始化 Bitmap 类的新实例 Bitmap s_bitmap = new Bitmap(s_image, s_imagewidth, s_imageheight); s_image.Dispose(); //设置此 Bitmap 的分辨率[水平分辨率,垂直分辨率] s_bitmap.SetResolution(72f, 72f); /**/ ////从指定的 原图片 创建新 Graphics 对象 Graphics s_textgraphics = Graphics.FromImage(s_bitmap); try { if (this.WaterMarkText != null) { if (this.WaterMarkText.Trim().Length > 0) { //开始制作水印文字 //设置原图片的 对象呈现质量为消除锯齿的呈现 s_textgraphics.SmoothingMode = SmoothingMode.AntiAlias; //在指定位置并且按指定大小绘制 原图片 对象的指定部分[要绘制的 Image 对象,所绘制图像的位置和大小将图像进行缩放以适合该矩形,左上角的 x 坐标,左上角的 y 坐标,绘制的源图像部分的宽度,绘制的源图像部分的高度,将设备像素指定为度量单位] s_textgraphics.DrawImage(s_bitmap, new Rectangle(0, 0, s_imagewidth, s_imageheight), 0, 0, s_imagewidth, s_imageheight, GraphicsUnit.Pixel); // int[] fontsizeArray = new int[7] { 16, 14, 12, 10, 8, 6, 4 }; //保存水印文字的字体信息 Font wm_textfont = null; //保存在 水印文字 参数中指定的、用 font 参数绘制的字符串的大小(以像素为单位)。 SizeF wm_textsize = new SizeF(0, 0); for (int i = 0; i < fontsizeArray.Length; i++) { wm_textfont = new Font("arial", ((float)fontsizeArray[i]), FontStyle.Bold); //测量用 wm_textfont 对象绘制的指定字符串 wm_textsize = s_textgraphics.MeasureString(this.WaterMarkText, wm_textfont); //判断水印文字是否大于 原图片 的宽度 if (((ushort)wm_textsize.Width) < ((ushort)s_imagewidth)) { break; } } fontsizeArray = null; float y = (((float)(s_imageheight - ((int)(((double)s_imageheight) * 0.05f)))) - (wm_textsize.Height / 2f)); float x = ((float)(s_imagewidth / 2)); //设置 水印文字 在布局矩形中居中对齐 StringFormat wm_textformat = new StringFormat(); wm_textformat.Alignment = StringAlignment.Center; //绘制 水印文字 的阴影 s_textgraphics.DrawString(this.WaterMarkText, wm_textfont, new SolidBrush(Color.FromArgb(153, 0, 0, 0)), new PointF((x + 1f), (y + 1f)), wm_textformat); //绘制 水印文字 s_textgraphics.DrawString(this.WaterMarkText, wm_textfont, new SolidBrush(Color.FromArgb(this.TextDiaphaneity, 255, 255, 255)), new PointF(x, y), wm_textformat); wm_textformat.Dispose(); } } } catch (System.Exception e) { throw e; } finally { s_textgraphics.Dispose(); } #endregion if (this.WaterMarkImagePath == null) { Random rd = new Random(); string fileName = FileName; //新文件名 string path = HttpContext.Current.Server.MapPath("~/" + savepath); //保存路径 s_bitmap.Save(string.Concat(path, "\\", fileName)); //删除原始图片 //IOHelper.DeleteFile(HttpContext.Current.Server.MapPath("~/" + this.SourceImagePath)); return(savepath + "/" + fileName); } if (this.WaterMarkImagePath.Trim() == System.String.Empty) { Random rd = new Random(); string fileName = FileName; //新文件名 string path = HttpContext.Current.Server.MapPath("~/" + savepath); //保存路径 s_bitmap.Save(string.Concat(path, "\\", fileName)); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //删除原始图片 //IOHelper.DeleteFile(HttpContext.Current.Server.MapPath("~/" + this.SourceImagePath)); return(savepath + "/" + fileName); } //从 水印图片 创建 Image 对象 Image wm_image = Image.FromFile(HttpContext.Current.Server.MapPath("~/" + this.WaterMarkImagePath)); int wm_imagewidth = wm_image.Width; //num3 int wm_imageheight = wm_image.Height; //num4 if (s_imagewidth < wm_imagewidth || s_imageheight < (wm_imageheight * 2)) { Random rd = new Random(); string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + "_" + rd.Next().ToString() + sExt; //新文件名 string path = HttpContext.Current.Server.MapPath("~/" + savepath); //保存路径 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } s_bitmap.Save(string.Concat(path, "\\", fileName)); //删除原始图片 //IOHelper.DeleteFile(HttpContext.Current.Server.MapPath("~/" + this.SourceImagePath)); return(savepath + "/" + fileName); } Bitmap s_bitmap2 = new Bitmap(s_bitmap); s_bitmap.Dispose(); //设置分辨率 s_bitmap2.SetResolution(s_imageHorizontalResolution, s_imageVerticalResolution); Graphics wm_imagegraphics = Graphics.FromImage(s_bitmap2); ImageAttributes wm_imageattributes = new ImageAttributes(); /**/ ////使用颜色重新映射表来调整图像颜色 ColorMap map = new ColorMap(); map.OldColor = Color.FromArgb(255, 0, 255, 0); map.NewColor = Color.FromArgb(0, 0, 0, 0); //为 水印图片 设置颜色重新映射表 wm_imageattributes.SetRemapTable(new ColorMap[] { map }, ColorAdjustType.Bitmap); //为 水印图片 设置颜色调整矩阵 wm_imageattributes.SetColorMatrix(new ColorMatrix(new float[][] { new float[] { 1f, 0, 0, 0, 0 }, new float[] { 0, 1f, 0, 0, 0 }, new float[] { 0, 0, 1f, 0, 0 }, new float[] { 0, 0, 0, this.ImageDeaphaneity, 0 }, new float[] { 0, 0, 0, 0, 1f } }), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); //int wm_x; //int wm_y; //GetWaterMarkPosition(ImageAlign.RightBottom, out wm_x, out wm_y, s_imagewidth, s_imageheight, wm_imagewidth, wm_imageheight); //在 原图片 上按指定大小绘制 水印图片 对象的指定部分 wm_imagegraphics.DrawImage(wm_image, new Rectangle(wm_x, wm_y, wm_imagewidth, wm_imageheight), 0, 0, wm_imagewidth, wm_imageheight, GraphicsUnit.Pixel, wm_imageattributes); wm_imageattributes.ClearColorMatrix(); wm_imageattributes.ClearRemapTable(); wm_image.Dispose(); wm_imagegraphics.Dispose(); try { //将 绘制水印图片后的图片 以指定格式并用指定的编解码参数保存到指定文件 Random rd = new Random(); string fileName = FileName; //新文件名 string path = HttpContext.Current.Server.MapPath("~/" + savepath); //保存路径 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } s_bitmap2.Save(string.Concat(path, "\\", fileName)); //删除原始图片 //IOHelper.DeleteFile(HttpContext.Current.Server.MapPath("~/" + this.SourceImagePath)); return(savepath + "/" + fileName); } catch (System.Exception e) { throw e; } finally { s_bitmap2.Dispose(); } }