internal WatermarkBase(Document A_0, WatermarkType A_1) : base(A_0) { this.bool_7 = true; this.bool_8 = true; this.bool_9 = true; this.watermarkType_0 = A_1; }
/// <summary> /// 添加水印(分图片水印与文字水印两种) /// </summary> /// <param name="oPath">原图片绝对地址</param> /// <param name="nPath">新图片放置的绝对地址</param> /// <param name="wType">要添加的水印的类型</param> /// <param name="content">水印内容,若添加文字水印,此即为要添加的文字; /// 若要添加图片水印,此为图片的路径</param> public Watermark(string oPath, string nPath, WatermarkType wType, string content) { oldPath = oPath; newPath = nPath; watermarkType = wType; sWaterMarkContent = content; }
protected override void ReadXmlAttributes(IXDLSAttributeReader reader) { int num = 6; base.ReadXmlAttributes(reader); if (reader.HasAttribute(BookmarkStart.b("笫伭䐯圱䘳嬵夷䠹圻樽㤿㉁⅃", 6))) { this.watermarkType_0 = (WatermarkType)reader.ReadEnum(BookmarkStart.b("笫伭䐯圱䘳嬵夷䠹圻樽㤿㉁⅃", num), typeof(WatermarkType)); } }
/// <summary>Get Image with Watermark</summary> public Image ApplyWatermark(Image img, NameParser parser, WatermarkType watermarkType) { switch (watermarkType) { default: case WatermarkType.NONE: return img; case WatermarkType.TEXT: return DrawWatermark(img, parser.Convert(Config.WatermarkText)); case WatermarkType.IMAGE: return DrawImageWatermark(img, Config.WatermarkImageLocation); } }
/// <summary>Get Image with Watermark</summary> public Image ApplyWatermark(Image img, NameParser parser, WatermarkType watermarkType) { switch (watermarkType) { default: case WatermarkType.NONE: return(img); case WatermarkType.TEXT: return(DrawWatermark(img, parser.Convert(Config.WatermarkText))); case WatermarkType.IMAGE: return(DrawImageWatermark(img, Config.WatermarkImageLocation)); } }
/// <summary>Get Image with Watermark</summary> public static Image ApplyWatermark(Image img, WatermarkType watermarkType) { switch (watermarkType) { default: case WatermarkType.NONE: return(img); case WatermarkType.TEXT: return(DrawWatermark(img, NameParser.Convert(new NameParserInfo(NameParserType.Watermark, Engine.conf.WatermarkText) { IsPreview = true, Picture = img }))); case WatermarkType.IMAGE: return(DrawImageWatermark(img, Engine.conf.WatermarkImageLocation)); } }
/// <summary> /// 将指定图片文件加上水印 /// </summary> /// <param name="filename"></param> /// <param name="wType">水印类型</param> /// <param name="wLocation">水印位置</param> /// <param name="Text">水印文字或图片地址。wType为“文字水印”时则为文字内容,否则则为水印图片地址</param> public static void WatermarkImage(String filename, WatermarkType wType, WatermarkLocation wLocation, Font font, Color color, String Text) { String _fileName = HttpContext.Current.Server.MapPath(filename); if (File.Exists(_fileName)) { //创建一个bitmap类型的bmp变量来读取文件。 Bitmap bmp = new Bitmap(_fileName); //新建第二个bitmap类型的bmp2变量,我这里是根据我的程序需要设置的。 Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height, bmp.PixelFormat); //将第一个bmp拷贝到bmp2中 Graphics draw = Graphics.FromImage(bmp2); draw.DrawImage(bmp, 0, 0); draw.Dispose(); bmp.Dispose();//释放bmp文件资源 bmp2 = GetWatermarkImage(bmp2, wType, wLocation, font, color, Text); bmp2.Save(_fileName); } }
public static bool IsMetricInDanger <T>(this T metricValue, T watermark, WatermarkType watermarkType) where T : IComparable { switch (watermarkType) { case WatermarkType.More: return(metricValue.CompareTo(watermark) == 1); case WatermarkType.Less: return(metricValue.CompareTo(watermark) == -1); case WatermarkType.MoreOrEqual: return(metricValue.CompareTo(watermark) > -1); case WatermarkType.LessOrEqual: return(metricValue.CompareTo(watermark) < 1); default: throw new ArgumentOutOfRangeException(nameof(watermarkType), watermarkType, null); } }
/// <summary> /// 添加水印 /// </summary> /// <param name="type">添加水印类型 WatermarkType</param> /// <param name="g">Graphics 实例</param> /// <param name="x">位置x</param> /// <param name="y">位置y</param> public static void AddWatermarkImage(string fileName, string imageRuntimePath, Bitmap image, WatermarkType type, Graphics g) { string ApplicationPhyPath = HttpContext.Current.Request.ServerVariables ["APPL_PHYSICAL_PATH"].Substring(HttpContext.Current.Request.ServerVariables ["APPL_PHYSICAL_PATH"].Length - 1, 1) != "\\" ? HttpContext.Current.Request.ServerVariables ["APPL_PHYSICAL_PATH"] : HttpContext.Current.Request.ServerVariables ["APPL_PHYSICAL_PATH"].Substring(0, HttpContext.Current.Request.ServerVariables ["APPL_PHYSICAL_PATH"].Length - 1); string ImageFile = string.Empty; string ImageEJJJFile = string.Empty; float s = 0F; switch (type) { case WatermarkType.Standard: //? x ? ImageFile = ApplicationPhyPath + WaitCheckWaterMark; ImageEJJJFile = ApplicationPhyPath + EJJJRemark; s = 0.5F; break; case WatermarkType.Small: //72 x 9 ImageEJJJFile = ApplicationPhyPath + EJJJSmallRemark; s = 0.2F; break; case WatermarkType.Smaller: //41 x 9 case WatermarkType.ExtraSmall: //? x ? break; } if (type != WatermarkType.Small) { using (Bitmap theIconBmp = new Bitmap(System.Drawing.Image.FromFile(ImageFile))) { int x = (image.Width - theIconBmp.Width) / 2; int y = (image.Height - theIconBmp.Height) / 2; float [] [] ptsArray = { new float [] { 1, 0, 0, 0, 0 }, new float [] { 0, 1, 0, 0, 0 }, new float [] { 0, 0, 1, 0, 0 }, new float [] { 0, 0, 0, s, 0 }, new float [] { 0, 0, 0, 0, 1 } }; ColorMatrix clrMatrix = new ColorMatrix(ptsArray); ImageAttributes imgAttributes = new ImageAttributes(); imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); //处理透明标记,暂时不用 //theIconBmp.MakeTransparent(Color.FromArgb(255, 255, 0, 0)); g.DrawImage(theIconBmp, new Rectangle(x, y, theIconBmp.Width, theIconBmp.Height), 0, 0, theIconBmp.Width, theIconBmp.Height, GraphicsUnit.Pixel, imgAttributes); } } using (Bitmap theDownIconBmp = new Bitmap(System.Drawing.Image.FromFile(ImageEJJJFile))) { int x = image.Width - theDownIconBmp.Width; int y = image.Height - theDownIconBmp.Height; float [] [] ptsArray = { new float [] { 1, 0, 0, 0, 0 }, new float [] { 0, 1, 0, 0, 0 }, new float [] { 0, 0, 1, 0, 0 }, new float [] { 0, 0, 0, s, 0 }, new float [] { 0, 0, 0, 0, 1 } }; ColorMatrix clrMatrix = new ColorMatrix(ptsArray); ImageAttributes imgAttributes = new ImageAttributes(); imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); //处理透明标记,暂时不用 //theIconBmp.MakeTransparent(Color.FromArgb(255, 255, 0, 0)); g.DrawImage(theDownIconBmp, new Rectangle(x, y, theDownIconBmp.Width, theDownIconBmp.Height), 0, 0, theDownIconBmp.Width, theDownIconBmp.Height, GraphicsUnit.Pixel, imgAttributes); } #region 输出图片 HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ContentType = "image/jpeg"; EncoderParameters myp = new EncoderParameters(1); myp.Param [0] = new EncoderParameter(Encoder.Quality, 90L); ImageCodecInfo [] encoders = ImageCodecInfo.GetImageEncoders(); image.Save(HttpContext.Current.Response.OutputStream, encoders [1], myp); #endregion if (!Directory.Exists(imageRuntimePath)) { Directory.CreateDirectory(imageRuntimePath); } if (type != WatermarkType.Small) { image.Save(imageRuntimePath + fileName + ".jpg", ImageFormat.Jpeg); } else { image.Save(imageRuntimePath + fileName + "_s.jpg", ImageFormat.Jpeg); } image.Dispose(); HttpContext.Current.Response.Clear(); }
/// <summary> /// 加水印文字 /// </summary> /// <param name="picture">imge</param> /// <param name="text">水印文字内容</param> /// <param name="markType">水印位置</param> /// <param name="font">文字</param> public static void AddWatermark(Image picture, string text, WatermarkType markType, Font font) { SizeF sizef = new SizeF(); using (var graphics = Graphics.FromImage(picture)) { if (font == null) { font = new Font("arial", 16, FontStyle.Bold); } sizef = graphics.MeasureString(text, font); float xpos = 0; float ypos = 0; switch (markType) { case WatermarkType.左上: { xpos = picture.Width * (float).01; ypos = picture.Height * (float).01; } break; case WatermarkType.中上: { xpos = (picture.Width * (float).50) - (sizef.Width / 2); ypos = picture.Height * (float).01; } break; case WatermarkType.右上: { xpos = (picture.Width * (float).99) - sizef.Width; ypos = picture.Height * (float).01; } break; case WatermarkType.左中: { xpos = picture.Width * (float).01; ypos = (picture.Height * (float).50) - (sizef.Height / 2); } break; case WatermarkType.中中: { xpos = (picture.Width * (float).50) - (sizef.Width / 2); ypos = (picture.Height * (float).50) - (sizef.Height / 2); } break; case WatermarkType.右中: { xpos = (picture.Width * (float).99) - sizef.Width; ypos = (picture.Height * (float).50) - (sizef.Height / 2); } break; case WatermarkType.左下: { xpos = picture.Width * (float).01; ypos = (picture.Height * (float).99) - sizef.Height; } break; case WatermarkType.中下: { xpos = (picture.Width * (float).50) - (sizef.Width / 2); ypos = (picture.Height * (float).99) - sizef.Height; } break; case WatermarkType.右下: { xpos = (picture.Width * (float).99) - sizef.Width; ypos = (picture.Height * (float).99) - sizef.Height; } break; } using (var format = new StringFormat() { Alignment = StringAlignment.Near }) using (var brush_1 = new SolidBrush(Color.FromArgb(153, 0, 0, 0))) using (var brush_2 = new SolidBrush(Color.FromArgb(153, 255, 255, 255))) { graphics.DrawString(text, font, brush_1, xpos, ypos, format); graphics.DrawString(text, font, brush_2, xpos, ypos, format); } } }
/// <summary> /// 加水印图片 /// </summary> /// <param name="picture">imge</param> /// <param name="watermark">水印文字图片</param> /// <param name="markType">水印位置</param> public static void AddWatermark(Image picture, Image watermark, WatermarkType markType) { int xpos = 0; int ypos = 0; switch (markType) { case WatermarkType.左上: { xpos = (int)(picture.Width * (float).01); ypos = (int)(picture.Height * (float).01); } break; case WatermarkType.中上: { xpos = (int)(picture.Width * (float).50) - (watermark.Width / 2); ypos = (int)(picture.Height * (float).01); } break; case WatermarkType.右上: { xpos = (int)(picture.Width * (float).99) - (watermark.Width / 2); ypos = (int)(picture.Height * (float).01); } break; case WatermarkType.左中: { xpos = (int)(picture.Width * (float).01); ypos = (int)(picture.Height * (float).50) - (watermark.Height / 2); } break; case WatermarkType.中中: { xpos = (int)(picture.Width * (float).50) - (watermark.Width / 2); ypos = (int)(picture.Height * (float).50) - (watermark.Height / 2); } break; case WatermarkType.右中: { xpos = (int)(picture.Width * (float).99) - watermark.Width; ypos = (int)(picture.Height * (float).50) - (watermark.Height / 2); } break; case WatermarkType.左下: { xpos = (int)(picture.Width * (float).01); ypos = (int)(picture.Height * (float).99) - watermark.Height; } break; case WatermarkType.中下: { xpos = (int)(picture.Width * (float).50) - (watermark.Width / 2); ypos = (int)(picture.Height * (float).99) - watermark.Height; } break; case WatermarkType.右下: { xpos = (int)(picture.Width * (float).99) - watermark.Width; ypos = (int)(picture.Height * (float).99) - watermark.Height; } break; } using (var graphics = Graphics.FromImage(picture)) using (var imageAttributes = new ImageAttributes()) { var colorMap = new ColorMap() { OldColor = Color.FromArgb(255, 0, 255, 0), NewColor = Color.FromArgb(0, 0, 0, 0) }; imageAttributes.SetRemapTable(new[] { colorMap }, ColorAdjustType.Bitmap); var colorMatrix = new ColorMatrix(colorMatrixElements); graphics.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes); } }
/// <summary> /// 添加图片水印 /// </summary> /// <param name="oldpath">原图片绝对地址</param> /// <param name="newpath">新图片放置的绝对地址</param> /// <param name="waterType">水印类型</param> /// <param name="isBig">true:大图,false:小图</param> public void addWaterMark(string oldpath, string newpath, WatermarkPosition watermarkPos, WatermarkType waterType, bool isBig) { try { System.Drawing.Image image = System.Drawing.Image.FromFile(oldpath); Bitmap b = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(b); g.Clear(Color.White); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.High; g.DrawImage(image, 0, 0, image.Width, image.Height); switch (waterType) { //是图片的话 case WatermarkType.WM_IMAGE: this.addWatermarkImage(g, WatermarkInfo.WatermarkImage, watermarkPos, image.Width, image.Height); break; //如果是文字 case WatermarkType.WM_TEXT: this.addWatermarkText(g, WatermarkInfo.WatermarkText, watermarkPos, image.Width, image.Height, isBig); break; } b.Save(newpath); b.Dispose(); image.Dispose(); } catch { if (File.Exists(oldpath)) { File.Delete(oldpath); } } finally { if (File.Exists(oldpath)) { File.Delete(oldpath); } } }
/// <summary> /// 生成水印图片 /// </summary> /// <param name="bmp">原图</param> /// <param name="wType">水印类型</param> /// <param name="wLocation">水印位置</param> /// <param name="Text">水印文字或图片地址。wType为“文字水印”时则为文字内容,否则则为水印图片地址</param> /// <returns></returns> protected static Bitmap GetWatermarkImage(Bitmap bmp, WatermarkType wType, WatermarkLocation wLocation, Font font, Color color, String Text) { Bitmap _bmp = bmp; using (Graphics g = Graphics.FromImage(_bmp)) { int w = _bmp.Width; //原图宽度 int h = _bmp.Height; //原图高度 SizeF s = g.MeasureString(Text, font); //文字所占大小 Image img = null; if (wType == WatermarkType.图片水印) { String fileName = HttpContext.Current.Server.MapPath(Text); if (File.Exists(fileName)) { img = Image.FromFile(fileName); if (img.Width > w || img.Height > h) { g.Dispose(); return(_bmp); } s = new SizeF(img.Width, img.Height); } else { g.Dispose(); return(_bmp); } } else { if (s.Width > w || s.Height > h) { return(_bmp); } } Point p = new Point(0, 0); switch (wLocation) { case WatermarkLocation.左: p = new Point(5, 5); break; case WatermarkLocation.中: p = new Point((w - (int)s.Width) / 2, 5); break; case WatermarkLocation.右: p = new Point((w - (int)s.Width) - 5, 5); break; case WatermarkLocation.中左: p = new Point(5, (h - (int)s.Height) / 2); break; case WatermarkLocation.中中: p = new Point((w - (int)s.Width) / 2, (h - (int)s.Height) / 2); break; case WatermarkLocation.中右: p = new Point((w - (int)s.Width) - 5, (h - (int)s.Height) / 2); break; case WatermarkLocation.左: p = new Point(5, (h - (int)s.Height) - 5); break; case WatermarkLocation.中: p = new Point((w - (int)s.Width) / 2, (h - (int)s.Height) - 5); break; case WatermarkLocation.右: p = new Point((w - (int)s.Width) - 5, (h - (int)s.Height) - 5); break; } g.CompositingMode = CompositingMode.SourceOver; g.CompositingQuality = CompositingQuality.HighQuality; if (wType == WatermarkType.图片水印) { float[][] ptsArray = { new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, 0.5f, 0 }, //注意:此处为0.5f,图像为半透明 new float[] { 0, 0, 0, 0, 1 } }; ColorMatrix clrMatrix = new ColorMatrix(ptsArray); ImageAttributes imgAttributes = new ImageAttributes(); //设置图像的颜色属性 imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); g.DrawImage(img, new Rectangle(p.X, p.Y, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttributes); img.Dispose(); } else { g.DrawString(Text, font, new SolidBrush(color), p); } } return(_bmp); }
internal WatermarkBase(WatermarkType A_0) : this(null, A_0) { }
/// <summary>Get Image with Watermark</summary> public static Image ApplyWatermark(Image img, WatermarkType watermarkType) { switch (watermarkType) { default: case WatermarkType.NONE: return img; case WatermarkType.TEXT: return DrawWatermark(img, NameParser.Convert(new NameParserInfo(NameParserType.Watermark, Engine.conf.WatermarkText) { IsPreview = true, Picture = img })); case WatermarkType.IMAGE: return DrawImageWatermark(img, Engine.conf.WatermarkImageLocation); } }