【原创】使编辑器CuteEditor上传自动添加水印(仅代码)
1、InsertImage.aspx中添加如下代码,以在前台显示该功能
<asp:CheckBox ID="cbx_wm" runat="server" Text="加水印" />
2、添加水印的方法
View Code
1 /// <summary> 2 /// 添加水印 3 /// </summary> 4 /// <param name="data">图片的byte数组表示</param> 5 /// <param name="waterFile">水印图片的地址</param> 6 7 public void MarkWater(ref byte[] data, string waterFile) 8 { 9 #region 处理原图 10 //原图对象 11 System.Drawing.Image sourceImage = null; 12 //备用:如果图片是索引的格式的则采用将此图片先clone到一张BMP上 13 Bitmap sourceBmp = null; 14 15 Bitmap bmPhoto = null; 16 //加水印后的对象 17 System.Drawing.Bitmap drawedImage = null; 18 //GDI+ 对象,用于绘制图形 19 System.Drawing.Graphics grPhoto = null; 20 //水印图片 21 System.Drawing.Image wmImage = null; 22 try 23 { 24 //由byte[]初始化原始图 25 //首先先将data转化成MemoryStream,在将MemoryStream得到Image 26 sourceImage = System.Drawing.Image.FromStream(this.ByteToStream(data)); 27 int sourceWidth = sourceImage.Width; 28 int sourceHeight = sourceImage.Height; 29 //如果原图是索引格式的 30 if (IsPixelFormatIndexed(sourceImage.PixelFormat)) 31 { 32 sourceBmp = new Bitmap(sourceWidth, sourceHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 33 using (Graphics g = Graphics.FromImage(sourceBmp)) 34 { 35 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 36 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 37 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 38 g.DrawImage(sourceBmp, 0, 0); 39 } 40 } 41 //建立一个bitmap,和需要加水印的图片一样大小 42 bmPhoto = new Bitmap(sourceWidth, sourceHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 43 44 //SetResolution:设置此 Bitmap 的分辨率 45 //这里直接将需要添加水印的图片的分辨率赋给了bitmap 46 bmPhoto.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution); 47 48 //Graphics:封装一个 GDI+ 绘图图面。 49 grPhoto = Graphics.FromImage(bmPhoto); 50 51 //设置图形的品质,这里设定消除锯齿的呈现 52 grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 53 54 //将要添加水印的图片按照原始大小描绘(复制)到图形中,如果图片是索引像素格式的,则使用bmp 55 grPhoto.DrawImage( 56 sourceBmp == null ? sourceImage : sourceBmp, // 要添加水印的图片 57 new Rectangle(0, 0, sourceWidth, sourceHeight), // 根据要添加的水印图片的宽和高 58 0, // X方向从0点开始描绘 59 0, // Y方向 60 sourceWidth, // X方向描绘长度 61 sourceHeight, // Y方向描绘长度 62 GraphicsUnit.Pixel); // 描绘的单位,这里用的是像素 63 #endregion 64 65 #region 处理水印图片 66 //根据指定路径初始化水印Img对象 67 wmImage = System.Drawing.Image.FromFile(waterFile); 68 //水印图片的宽和高 69 int wmWidth = wmImage.Width; 70 int wmHeight = wmImage.Height; 71 //再由bmPhoto构造一个Bitmap对象bmWatermark 72 Bitmap bmWatermark = new Bitmap(bmPhoto); 73 //设置bmWatermark对象的分辨率 74 bmWatermark.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution); 75 76 Graphics grWatermark = Graphics.FromImage(bmWatermark); 77 78 System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); 79 80 System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); 81 82 colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); 83 colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); 84 85 System.Drawing.Imaging.ColorMap[] remapTable = { colorMap }; 86 87 imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap); 88 89 float[][] colorMatrixElements = { 90 new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, 91 new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, 92 new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, 93 new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f}, 94 new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}}; 95 System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); 96 97 imageAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, 98 System.Drawing.Imaging.ColorAdjustType.Bitmap); 99 100 //水印图片的位置 101 int xPosOfWm = ((sourceWidth - wmWidth) - 10); 102 int yPosOfWm = ((sourceHeight - wmHeight) - 10); 103 104 grWatermark.DrawImage(wmImage, 105 new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), //Set the detination Position 106 0, // x-coordinate of the portion of the source image to draw. 107 0, // y-coordinate of the portion of the source image to draw. 108 wmWidth, // Watermark Width 109 wmHeight, // Watermark Height 110 GraphicsUnit.Pixel, // Unit of measurment 111 imageAttributes); //ImageAttributes Object 112 #endregion 113 sourceImage = bmWatermark; 114 //将加了水印的图片转成byte数组 115 System.IO.MemoryStream ms = new MemoryStream(); 116 sourceImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 117 data = ms.ToArray(); 118 ms = null; 119 sourceImage = null; 120 bmPhoto = null; 121 bmWatermark = null; 122 sourceBmp = null; 123 grPhoto = null; 124 grWatermark = null; 125 126 } 127 catch (Exception Ex) 128 { 129 130 //throw; 131 } 132 133 }
3、辅助方法-将bype数组改成 MemoryStream
View Code
1 /// <summary> 2 /// 将bype数组改成 MemoryStream 3 /// </summary> 4 /// <param name="mybyte"></param> 5 /// <returns></returns> 6 7 private System.IO.MemoryStream ByteToStream(byte[] mybyte) 8 { 9 System.IO.MemoryStream mymemorystream = new System.IO.MemoryStream(mybyte, 0, mybyte.Length); 10 return mymemorystream; 11 }
4、在CheckUploadData方法中添加如下代码
View Code
//添加水印 if (this.cbx_wm.Checked) { this.MarkWater(ref data, Server.MapPath("~/logo.png")); }
完成