/// <summary> /// 在图片上增加文字水印 系统默认方法 字体:Verdana,大小:20,颜色:Green /// </summary> /// <param name="sourcePath">The source path.</param> /// <param name="newPath">The new path.</param> /// <param name="letter">水印的文字</param> /// <param name="positionType">图片水印位置:UL,UM , UR , ML , MM , MR , BL , BM , BR ,CUSTORM 为自定义</param> /// <returns></returns> public static bool TextWatermark(string sourcePath, string newPath, string letter, PicWaterMarkPosition positionType) { System.Drawing.Font f = new System.Drawing.Font("Verdana", 20); System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green); return TextWatermark(sourcePath, newPath, letter, f, b, 0, 0, positionType); }
/// <summary> /// 在图片上增加文字水印 /// </summary> /// <param name="Path">原服务器图片路径</param> /// <param name="Path_sy">生成的带文字水印的图片路径</param> /// <param name="letter">水印的文字</param> /// <param name="font">字体及大小</param> /// <param name="brush">画笔主要是颜色</param> /// <param name="txtPositionX">坐标X</param> /// <param name="txtPositionY">坐标Y</param> /// <param name="positionTpye">图片水印位置:UL,UM , UR , ML , MM , MR , BL , BM , BR ,CUSTORM 为自定义</param> /// <returns></returns> public static bool TextWatermark(string sourcePath, string newPath, string letter, System.Drawing.Font font, System.Drawing.Brush brush, int txtPositionX, int txtPositionY, PicWaterMarkPosition positionTpye) { string addText = letter; try { System.Drawing.Image image = System.Drawing.Image.FromFile(sourcePath); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image); g.DrawImage(image, 0, 0, image.Width, image.Height); System.Drawing.Font f = font; System.Drawing.Brush b = brush; switch (positionTpye) { case PicWaterMarkPosition.CUSTORM: g.DrawString(addText, f, b, txtPositionX, txtPositionY); break; case PicWaterMarkPosition.UL: g.DrawString(addText, f, b, 2, 2); break; case PicWaterMarkPosition.UM: g.DrawString(addText, f, b, image.Width / 2 - (letter.Length * font.Size) / 4, 2); break; case PicWaterMarkPosition.UR: g.DrawString(addText, f, b, image.Width - (float)letter.Length / 1.9f * font.Size, 2); break; case PicWaterMarkPosition.ML: g.DrawString(addText, f, b, 2, image.Height / 2 - font.Height / 4); break; case PicWaterMarkPosition.MM: g.DrawString(addText, f, b, image.Width / 2 - (letter.Length * font.Size) / 4, image.Height / 2 - font.Height / 4); break; case PicWaterMarkPosition.MR: // x.r y.m g.DrawString(addText, f, b, image.Width - (float)letter.Length / 1.9f * font.Size, image.Height / 2 - font.Height / 4); break; case PicWaterMarkPosition.BL: //x.l y.b g.DrawString(addText, f, b, 2, image.Height - font.Height / 1.3f); break; case PicWaterMarkPosition.BM: // x.m y.b g.DrawString(addText, f, b, image.Width / 2 - (letter.Length * font.Size) / 4, image.Height - font.Height / 1.3f); break; case PicWaterMarkPosition.BR: //x.r y.b g.DrawString(addText, f, b, image.Width - (float)letter.Length / 1.9f * font.Size, image.Height - font.Height / 1.3f); break; default: break; } g.Dispose(); try { image.Save(newPath); image.Dispose(); return true; } catch (Exception) { image.Dispose(); return false; } } catch (Exception) { return false; } }
/// <summary> /// 在图片上生成图片水印 /// </summary> /// <param name="sourcePath">原图片路径</param> /// <param name="targetPath">另存为路径</param> /// <param name="waterMarkFilePath">水印图片路径</param> /// <param name="positionType">图片水印位置:UL,UM , UR , ML , MM , MR , BL , BM , BR ,CUSTORM 为自定义.</param> /// <param name="quality">是否是高质量图片 取值范围0--100</param> /// <param name="watermarkTransparency">图片水印透明度 取值范围1--10 (10为不透明)</param> /// <returns></returns> public static bool PictureWatermark(string sourcePath, string targetPath, string waterMarkFilePath, PicWaterMarkPosition positionType, int quality, int watermarkTransparency) { System.Drawing.Image img = System.Drawing.Image.FromFile(sourcePath); System.Drawing.Bitmap outPut = new System.Drawing.Bitmap(img); try { #region using using (Graphics g = Graphics.FromImage(outPut)) { //设置高质量插值法 //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; System.Drawing.Image markImg = new System.Drawing.Bitmap(waterMarkFilePath); if (markImg.Height >= img.Height || markImg.Width >= img.Width) { return false; } System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = System.Drawing.Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = System.Drawing.Color.FromArgb(0, 0, 0, 0); System.Drawing.Imaging.ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap); float transparency = 0.5F; if (watermarkTransparency >= 1 && watermarkTransparency <= 10) { transparency = (watermarkTransparency / 10.0F); } 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, transparency, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); int xpos = 0; int ypos = 0; #region position switch (positionType) { case PicWaterMarkPosition.UL: xpos = (int)(img.Width * (float).01); ypos = (int)(img.Height * (float).01); break; case PicWaterMarkPosition.UM: xpos = (int)((img.Width * (float).50) - (markImg.Width / 2)); ypos = (int)(img.Height * (float).01); break; case PicWaterMarkPosition.UR: xpos = (int)((img.Width * (float).99) - (markImg.Width)); ypos = (int)(img.Height * (float).01); break; case PicWaterMarkPosition.ML: xpos = (int)(img.Width * (float).01); ypos = (int)((img.Height * (float).50) - (markImg.Height / 2)); break; case PicWaterMarkPosition.MM: xpos = (int)((img.Width * (float).50) - (markImg.Width / 2)); ypos = (int)((img.Height * (float).50) - (markImg.Height / 2)); break; case PicWaterMarkPosition.MR: xpos = (int)((img.Width * (float).99) - (markImg.Width)); ypos = (int)((img.Height * (float).50) - (markImg.Height / 2)); break; case PicWaterMarkPosition.BL: xpos = (int)(img.Width * (float).01); ypos = (int)((img.Height * (float).99) - markImg.Height); break; case PicWaterMarkPosition.BM: xpos = (int)((img.Width * (float).50) - (markImg.Width / 2)); ypos = (int)((img.Height * (float).99) - markImg.Height); break; case PicWaterMarkPosition.BR: xpos = (int)((img.Width * (float).99) - (markImg.Width)); ypos = (int)((img.Height * (float).99) - markImg.Height); break; } #endregion position g.DrawImage(markImg, new System.Drawing.Rectangle(xpos, ypos, markImg.Width, markImg.Height), 0, 0, markImg.Width, markImg.Height, System.Drawing.GraphicsUnit.Pixel, imageAttributes); System.Drawing.Imaging.ImageCodecInfo[] codecs = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); System.Drawing.Imaging.ImageCodecInfo ici = null; foreach (System.Drawing.Imaging.ImageCodecInfo codec in codecs) { if (codec.MimeType.IndexOf("jpeg") > -1) { ici = codec; } } System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters(); long[] qualityParam = new long[1]; if (quality < 0 || quality > 100) { quality = 80; } qualityParam[0] = quality; System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam); encoderParams.Param[0] = encoderParam; if (ici != null) { outPut.Save(targetPath, ici, encoderParams); } else { outPut.Save(targetPath); } g.Dispose(); markImg.Dispose(); imageAttributes.Dispose(); } #endregion using } catch (Exception) { return false; } finally { if (outPut != null) { outPut.Dispose(); } if (img != null) { img.Dispose(); } } return true; }