很多时候为了避免伪造和盗版需要为图片添加数字签名,
Aspose.Imaging提供的API可以帮助开发人员快速实现该功能,这篇文章介绍的实现方法主要是通过Graphics类来把签名的图片绘制到目标图片上,具体可以参考下面的详细步奏。
1.首先创建一个Image类的实例,然后从本地加载图片
using (Image canvas = Image.Load("D:/sample.jpg"))
{
using (Image signature = Image.Load("D:/signature.gif"))
{
2.创建和初始化一个Graphic对象
Graphics graphics = new Graphics(canvas);
3.把签名的图片绘制到目标图片
graphics.DrawImage(signature, new Point(canvas.Height - signature.Height, canvas.Width - signature.Width));
4.保存签名好的图片
canvas.Save("D:/output.png", new PngOptions());
该事例的完整代码如下:
//Create an instance of Image and load the primary image
using (Image canvas = Image.Load("D:/sample.jpg"))
{
//Create another instance of Image and load the secondary image containing the signature graphics
using (Image signature = Image.Load("D:/signature.gif"))
{
//Create an instance of Graphics class and initialize it using the object of the primary image
Graphics graphics = new Graphics(canvas);
//Call the DrawImage method while passing the instance of secondary image and appropriate location
//The following snippet tries to draw the secondary image at the right bottom of the primary image
graphics.DrawImage(signature, new Point(canvas.Height - signature.Height, canvas.Width - signature.Width));
//Save the result in PNG format
canvas.Save("D:/output.png", new PngOptions());
}
}