向图像添加水印

向图像添加水印

本文介绍如何使用Aspose.PSD向图像添加水印。向图像添加水印是图像处理应用程序的常见需求。该示例使用Graphics类在图像表面绘制字符串。

添加水印

为了演示该操作,我们将从磁盘加载一个BMP图像,并使用Graphics类的DrawString方法在图像表面上绘制一个字符串作为水印。我们将使用PngOptions类将图像保存为PNG格式。以下是演示如何向图像添加水印的代码示例。示例源代码已分为几个部分,以便于跟踪。逐步介绍示例展示了如何:

  1. 加载 一个图像。
  2. 创建并初始化一个Graphics对象。
  3. 创建并初始化Font和SolidBrush对象。
  4. 使用Graphics类的DrawString方法绘制字符串作为水印。
  5. 将图像保存为PNG。

以下代码片段展示了如何向图像添加水印。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
// Load a PSD file as an image and cast it into PsdImage
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "layers.psd"))
{
// Create graphics object to perform draw operations.
Graphics graphics = new Graphics(psdImage);
// Create font to draw watermark with.
Font font = new Font("Arial", 20.0f);
// Create a solid brush with color alpha set near to 0 to use watermarking effect.
using (SolidBrush brush = new SolidBrush(Color.FromArgb(50, 128, 128, 128)))
{
// Specify string alignment to put watermark at the image center.
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
// Draw watermark using font, partly-transparent brush and rotation matrix at the image center.
graphics.DrawString("Some watermark text", font, brush, new RectangleF(0, 0, psdImage.Width, psdImage.Height), sf);
}
// Export the image into PNG file format.
psdImage.Save(dataDir + "AddWatermark_output.png", new PngOptions());
}

添加对角线水印

向图像添加对角线水印与以上讨论的添加水平水印类似,但有一些不同之处。为了演示该操作,我们将从磁盘加载一个JPG图像,使用Matrix类的对象添加变换,并使用Graphics类的DrawString方法在图像表面上绘制一个字符串作为水印。以下是演示如何向图像添加对角线水印的代码示例。示例源代码已分为几个部分,以便于跟踪。逐步介绍示例展示了如何:

  1. 加载图像。
  2. 创建并初始化Graphics对象。
  3. 创建并初始化Font和SolidBrush对象。
  4. 使用SizeF对象获取图像大小。
  5. 创建Matrix类的实例并执行复合变换。
  6. 将变换分配给Graphics对象。
  7. 创建并初始化StringFormat对象。
  8. 使用Graphics类的DrawString方法绘制字符串作为水印。
  9. 保存结果图像。

以下代码片段展示了如何添加对角线水印。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
// Load a PSD file as an image and cast it into PsdImage
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "layers.psd"))
{
// Create graphics object to perform draw operations.
Graphics graphics = new Graphics(psdImage);
// Create font to draw watermark with.
Font font = new Font("Arial", 20.0f);
// Create a solid brush with color alpha set near to 0 to use watermarking effect.
using (SolidBrush brush = new SolidBrush(Color.FromArgb(50, 128, 128, 128)))
{
// specify transform matrix to rotate watermark.
graphics.Transform = new Matrix();
graphics.Transform.RotateAt(45, new PointF(psdImage.Width / 2, psdImage.Height / 2));
// Specify string alignment to put watermark at the image center.
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
// Draw watermark using font, partly-transparent brush at the image center.
graphics.DrawString("Some watermark text", font, brush, new RectangleF(0, psdImage.Height / 2, psdImage.Width, psdImage.Height / 2), sf);
}
// Export the image into PNG file format.
psdImage.Save(dataDir + "AddDiagnolWatermark_output.png", new PngOptions());
}