1 Bitmap bm = null; 2 ///图片+文字水印 3 System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("~/Images/xxx.png")); 4 5 string strContent = String.Format("@{0}", "你的文字内容"); 6 Font font = new Font("宋体", 12); 7 8 int fontWidth = strContent.Length * 16;//固定了文字的宽度。12大小的文字 用16宽度还行。 9 10 bm = new Bitmap(copyImage.Width + fontWidth, copyImage.Height); 11 Graphics tempg = Graphics.FromImage(bm); 12 Size fontSize = tempg.MeasureString(strContent, font).ToSize();//为了取文字的高度。 13 14 tempg.DrawImage(copyImage, new Rectangle(0, 0, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel); 15 tempg.DrawString(strContent, font, new SolidBrush(Color.FromArgb(255, 85, 85, 85)) 16 , copyImage.Width, bm.Height - fontSize.Height - 2);//文字阴影 17 tempg.DrawString(strContent, font, new SolidBrush(Color.FromArgb(255, Color.White)) 18 , copyImage.Width - 1, bm.Height - fontSize.Height - 3);//文字正文 19 20 tempg.Dispose(); 21 22 copyImage.Dispose(); 23 //bm就是生成的水印临时图 24
1 //取得图片对象,并使用流中嵌入的颜色管理信息 2 System.Drawing.Image myImage = System.Drawing.Image.FromStream(fromFileStream, true); 3 //新建一个画板 4 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myImage); 5 //设置高质量插值法 6 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 7 //设置高质量,低速度呈现平滑程度 8 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 9 10 #region 水印透明度 转换矩阵 11 float[][] nArray ={ new float[] {1, 0, 0, 0, 0}, 12 new float[] {0, 1, 0, 0, 0}, 13 new float[] {0, 0, 1, 0, 0}, 14 new float[] {0, 0, 0, 0.5f, 0}, //0.5f 就是转换矩阵中调整透明度的值。0.5就是50%透明。 15 new float[] {0, 0, 0, 0, 1} 16 }; 17 ColorMatrix matrix = new ColorMatrix(nArray); 18 ImageAttributes attributes = new ImageAttributes(); 19 attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); 20 #endregion 21 //添加水印 放在 图片的右下角 22 g.DrawImage(bm, new Rectangle(myImage.Width - bm.Width, myImage.Height - bm.Height, bm.Width, bm.Height), 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel, attributes);
添加水印处理逻辑:
1.临时生成水印临时图片
2.设置图片转换矩阵 即 设置水印临时图片透明度
3.给原图添加水印