/// <summary> /// 图片水印处理方法 /// </summary> /// <param name="waterpath">水印图片(绝对路径)</param> /// <param name="location">水印位置(传送正确的代码)</param> public ImageProcess ImageWatermark(string waterpath, EnumLocation location) { Bitmap waterimg = Image.FromFile(waterpath) as Bitmap; var waterimg2 = new Bitmap(waterimg.Width, waterimg.Height); var wid = waterimg.Width; var hei = waterimg.Height; for (int i = 0; i < wid; i++) { for (int j = 0; j < hei; j++) { var baseColor = waterimg.GetPixel(i, j); if (baseColor.A != 0) { baseColor = Color.FromArgb(ColorTransparent, baseColor.R, baseColor.G, baseColor.B); } waterimg2.SetPixel(i, j, baseColor); } } ArrayList loca = GetLocation(location, processImage, waterimg2.Width, waterimg2.Height); using (var g = Graphics.FromImage(processImage)) { g.DrawImage(waterimg2, new Rectangle(int.Parse(loca[0].ToString()), int.Parse(loca[1].ToString()), waterimg.Width, waterimg.Height)); waterimg.Dispose(); }; return this; }
/// <summary> /// 水印位置计算 /// </summary> /// <param name="location">水印位置</param> /// <param name="img">需要添加水印的图片</param> /// <param name="waterWidth">水印宽度</param> /// <param name="waterHeight">水印高度</param> private static ArrayList GetLocation(EnumLocation location, Image img, int waterWidth, int waterHeight) { ArrayList loca = new ArrayList(); int x = 0; int y = 0; if (location == EnumLocation.LeftTop) { x = Margin; y = Margin; } else if (location == EnumLocation.Top) { x = img.Width / 2 - waterWidth / 2; y = Margin; } else if (location == EnumLocation.RightTop) { x = img.Width - waterWidth - 10; y = Margin; } else if (location == EnumLocation.LeftCenter) { x = Margin; y = img.Height / 2 - waterHeight / 2; } else if (location == EnumLocation.Center) { x = img.Width / 2 - waterWidth / 2; y = img.Height / 2 - waterHeight / 2; } else if (location == EnumLocation.RightCenter) { x = img.Width - waterWidth - Margin; y = img.Height / 2 - waterHeight / 2; } else if (location == EnumLocation.LeftBottom) { x = Margin; y = img.Height - waterHeight - Margin; } else if (location == EnumLocation.Bottom) { x = img.Width / 2 - waterWidth / 2; y = img.Height - waterHeight - Margin; } else if (location == EnumLocation.RightBottom) { x = img.Width - waterWidth - Margin; y = img.Height - waterHeight - Margin; } loca.Add(x); loca.Add(y); return loca; }
/// <summary> /// 文字水印处理方法 /// </summary> /// <param name="font">字体</param> /// <param name="letter">水印文字</param> /// <param name="color">颜色</param> /// <param name="location">水印位置</param> public ImageProcess LetterWatermark(string letter, Font font, Color color, EnumLocation location) { Brush br = new SolidBrush(Color.FromArgb(ColorTransparent, color.R, color.G, color.B)); using (var g = Graphics.FromImage(processImage)) { var size = g.MeasureString(letter, font).ToSize(); ArrayList loca = GetLocation(location, processImage, size.Width, size.Height); g.DrawString(letter, font, br, float.Parse(loca[0].ToString()), float.Parse(loca[1].ToString())); } return this; }
public MissingTags(EnumLocation tagLocation) { Location = tagLocation; }