将水印添加到图像

将水印添加到图像

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

添加水印

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

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

下面的代码片段展示了如何在图像上添加水印。

String dataDir = Utils.getDataDir(AddWatermark.class) + "PSD/";
// Load a PSD file as an image and cast it into PsdImage
try (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.
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.setAlignment(StringAlignment.Center);
sf.setLineAlignment(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.getWidth(), psdImage.getHeight()), 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. 保存结果图像。

下面的代码片段展示了如何在图像上添加对角水印。

String dataDir = Utils.getDataDir(AddDiagnolWatermark.class) + "PSD/";
// Load a PSD file as an image and cast it into PsdImage
try (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.
SolidBrush brush = new SolidBrush(Color.fromArgb(50, 128, 128, 128));
// specify transform matrix to rotate watermark.
graphics.setTransform(new Matrix());
graphics.getTransform().rotateAt(45, new PointF(psdImage.getWidth() / 2, psdImage.getHeight() / 2));
// Specify string alignment to put watermark at the image center.
StringFormat sf = new StringFormat();
sf.setAlignment(StringAlignment.Center);
// Draw watermark using font, partly-transparent brush at the image center.
graphics.drawString("Some watermark text", font, brush, new RectangleF(0, psdImage.getHeight() / 2, psdImage.getWidth(), psdImage.getHeight() / 2), sf);
// Export the image into PNG file format.
psdImage.save(dataDir + "AddDiagnolWatermark_output.png", new PngOptions());
}