private void button1_Click(object sender, EventArgs e) { Tuple <bool, string> checkResult = ValidateData(); if (!checkResult.Item1) { MessageBox.Show(checkResult.Item2); return; } string outputType = radioJPG.Checked ? ".jpg" : ".png"; int paddingleft = 0; int paddingright = 0; int paddingtop = 0; int paddingbottom = 0; Single alpha = Convert.ToSingle(txtAlpha.Text); WatermarkDirection wmd = WatermarkDirection.TopLeft; if (radioLT.Checked) { wmd = WatermarkDirection.TopLeft; paddingleft = Convert.ToInt32(txtLTMarginLeft.Text); paddingtop = Convert.ToInt32(txtLTMarginTop.Text); } if (radioLB.Checked) { wmd = WatermarkDirection.BottomLeft; paddingleft = Convert.ToInt32(txtLBMarginLeft.Text); paddingbottom = Convert.ToInt32(txtLBMarginBottom.Text); } if (radioRT.Checked) { wmd = WatermarkDirection.TopRight; paddingtop = Convert.ToInt32(txtRTMarginTop.Text); paddingright = Convert.ToInt32(txtRTMarginRight.Text); } if (radioRB.Checked) { wmd = WatermarkDirection.BottomRight; paddingright = Convert.ToInt32(txtRBMarginRight.Text); paddingbottom = Convert.ToInt32(txtRBMarginBottom.Text); } try { ImageGenerator generator = new ImageGenerator(); string target = generator.BuildWatermark(txtOrginal.Text, txtWaterMark.Text, txtOutput.Text, outputType, chkMarkStyle.Checked, chkMartString.Checked, Convert.ToInt32(txtMarkWidth.Text), Convert.ToInt32(txtMarkHeight.Text), alpha, wmd, paddingleft, paddingtop, paddingright, paddingbottom, txtMarkText.Text); pictureBox1.Image = Image.FromFile(target); MessageBox.Show("水印添加成功"); } catch (Exception ex) { MessageBox.Show("添加水印时发生错误:" + ex.Message); } }
/// <summary> /// 创建带水印的图片 /// </summary> /// <param name="sourcePath">原图片地址</param> /// <param name="watermarkPath">水印图片路径</param> /// <param name="targetPath">输出路径</param> /// <param name="targetType">输出类型</param> /// <param name="styleEnabled">启用自定义格式</param> /// <param name="textEnabled">启用自定义文字</param> /// <param name="width">水印宽度</param> /// <param name="height">水印高度</param> /// <param name="direction">水印位置</param> /// <param name="paddingleft">距左</param> /// <param name="paddingtop">距上</param> /// <param name="paddingright">距右</param> /// <param name="paddingbottom">距下</param> /// <param name="text">水印文字</param> /// <returns>图片路径</returns> public string BuildWatermark(string sourcePath, string watermarkPath, string targetPath, string targetType, bool styleEnabled = false, bool textEnabled = false, int width = 50, int height = 50, Single alpha = 1.0f, WatermarkDirection direction = WatermarkDirection.TopLeft, int paddingleft = 0, int paddingtop = 0, int paddingright = 0, int paddingbottom = 0, string text = "") { //原图像 Image imgSource = Image.FromFile(sourcePath); int sourceWidth = imgSource.Width; int sourceHeight = imgSource.Height; //水印图像 Image imgWatermark = new Bitmap(watermarkPath); int wmWidth = imgWatermark.Width; int wmHeight = imgWatermark.Height; //原Bitmap Bitmap bmSource = new Bitmap(sourceWidth, sourceHeight, PixelFormat.Format24bppRgb); bmSource.SetResolution(72, 72); Graphics grSource = Graphics.FromImage(bmSource); grSource.SmoothingMode = SmoothingMode.AntiAlias; grSource.DrawImage(imgSource, new Rectangle(0, 0, sourceWidth, sourceHeight), 0, 0, sourceWidth, sourceHeight, GraphicsUnit.Pixel); if (textEnabled) { //找到适合的字体大小 int[] sizes = new int[] { 36, 24, 16, 14, 10, 6, 4 }; Font crFont = null; SizeF crSize = new SizeF(); for (int i = 0; i < 7; i++) { crFont = new Font("arial", sizes[i], FontStyle.Bold); crSize = grSource.MeasureString(text, crFont); if ((ushort)crSize.Width < (ushort)sourceWidth) { break; } } //计算X,Y坐标 int yPixlesFromBottom = (int)(sourceHeight * 0.05); float yPosFromBottom = ((sourceHeight - yPixlesFromBottom) - (crSize.Height / 2)); float xCenterOfSource = (sourceWidth / 2); //画出水印文字 StringFormat StrFormat = new StringFormat(); StrFormat.Alignment = StringAlignment.Center; SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255)); grSource.DrawString(text, crFont, semiTransBrush, new PointF(xCenterOfSource, yPosFromBottom), StrFormat); } //重新加载bitmap Bitmap bmWatermark = new Bitmap(bmSource); bmWatermark.SetResolution(imgSource.HorizontalResolution, imgSource.VerticalResolution); Graphics grWatermark = Graphics.FromImage(bmWatermark); //通过定义一个ImageAttributes 对象并设置它的两个属性,我们就是实现了两个颜色的处理,以达到半透明的水印效果。 //处理水印图象的第一步是把背景图案变为透明的(Alpha=0, R=0, G=0, B=0)。我们使用一个Colormap 和定义一个RemapTable来做这个。 //就像前面展示的,我的水印被定义为100%绿色背景,我们将搜到这个颜色,然后取代为透明。 ImageAttributes imageAttributes = new ImageAttributes(); ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap }; //第二个颜色处理用来改变水印的不透明性。 //通过应用包含提供了坐标的RGBA空间的5x5矩阵来做这个。 //通过设定第三行、第三列为0.3f我们就达到了一个不透明的水平。结果是水印会轻微地显示在图象底下一些。 imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); float[][] colorMatrixElements = { new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, alpha, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }; ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); int xPosOfWm = 0; int yPosOfWm = 0; switch (direction) { case WatermarkDirection.TopLeft: xPosOfWm = paddingleft; yPosOfWm = paddingtop; break; case WatermarkDirection.TopRight: xPosOfWm = sourceWidth - width - paddingright; yPosOfWm = paddingtop; break; case WatermarkDirection.BottomLeft: xPosOfWm = paddingleft; yPosOfWm = sourceHeight - height - paddingbottom; break; case WatermarkDirection.BottomRight: xPosOfWm = sourceWidth - width - paddingright; yPosOfWm = sourceHeight - height - paddingbottom; break; } grWatermark.DrawImage(imgWatermark, new Rectangle(xPosOfWm, yPosOfWm, width, height), 0, 0, wmWidth, wmHeight, GraphicsUnit.Pixel, imageAttributes); //最后的步骤将是使用新的Bitmap取代原来的Image。 销毁两个Graphic对象,然后把Image 保存到文件系统。 imgSource = bmWatermark; grSource.Dispose(); grWatermark.Dispose(); string filename = targetPath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + targetType; if (targetType == ".jpg") { imgSource.Save(filename, ImageFormat.Jpeg); } else { imgSource.Save(filename, ImageFormat.Png); } imgSource.Dispose(); imgWatermark.Dispose(); return(filename); }