先添加对ImageResizer的引用,
然后通过vs-工具-库程序包管理器-程序包管理器控制台,输入Install-Package ImageResizer.Plugins.Watermark安装水印插件,并添加引用,或直接引用dll。
引用命名空间(程序默认需要添加System.Drawing程序集的引用)
using ImageResizer;using ImageResizer.Configuration;using ImageResizer.Plugins.Watermark;
Config c = Config.Current;WatermarkPlugin wp = c.Plugins.Get();if (wp == null){ wp = new WatermarkPlugin(); wp.Install(c);}//创建一个图片图层,并配置 ImageLayer i = new ImageLayer(c);i.Path = "/image/watermark.png";i.Bottom = new DistanceUnit("10");i.Right = new DistanceUnit("10");i.Width = new DistanceUnit("50");//默认单位为px,还可以通过重载取百分比 wp.NamedWatermarks["img"] = new Layer[] { i }; c.CurrentImageBuilder.Build(strSourceImagepath, strOutputImagpath, new ResizeSettings("watermark=img&width=200&height=400&crop=auto"));
还可以添加文字水印,或者同时添加文字和图片水印
TextLayer t = new TextLayer();t.Fill = true;t.Text = "Hello #{name}";t.TextColor = System.Drawing.Color.SkyBlue;wp.NamedWatermarks["text"] = new Layer[] { t };c.CurrentImageBuilder.Build(strSourceImagepath, strOutputImagpath, new ResizeSettings("watermark=text;name=Lancer"));//同时添加图片和文字水印c.CurrentImageBuilder.Build(strSourceImagepath, strOutputImagpath, new ResizeSettings("watermark=img,text;name=Lancer&"));
支持的QueryString:(need over the wall)