当前位置: 首页 > 工具软件 > ImageResizer > 使用案例 >

ImageResizer为图片添加水印

劳麒
2023-12-01

先添加对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<WatermarkPlugin>();
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:http://imageresizing.net/docs/basics(need over the wall)

转载于:https://www.cnblogs.com/dc-lancer/archive/2012/12/25/2832071.html

 类似资料: