Adding a Watermark to an Image
Adding a Watermark to an Image
This document explains how to add a watermark to an image using Aspose.PSD. Adding a watermark to an image is a common requirement for image processing applications. This example uses the Graphics class to draw a string on the image surface.
Adding a Watermark
To demonstrate the operation, we will load a BMP image from disk and draw a string as the watermark on the image surface using the Graphics class DrawString method. We will save the image to PNG format using the PngOptions class. Below is a code example that demonstrates how to add a watermark to an image. The example source code has been split into parts to make it easy to follow. Step by step, the examples show how to:
- Load an image.
- Create and initialize a Graphics object.
- Create and initialize Font and SolidBrush objects.
- Draw a string as watermark using the Graphics class DrawString method.
- Save image to PNG.
The following code snippet shows you how to add watermark on the image.
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()); | |
} |
Adding a Diagonal Watermark
Adding a diagonal watermark to an image is similar to adding a horizontal watermark as discussed above, with a few differences. To demonstrate the operation, we will load a JPG image from disk, add transformations using an object of Matrix class and draw a string as the watermark on the image surface using the Graphics class DrawString method. Below is a code example that demonstrates how to add a diagonal watermark to an image. The example source code has been split into parts to make it easy to follow. Step by step, the examples show how to:
- Load an image.
- Create and initialize a Graphics object.
- Create and initialize Font and SolidBrush objects.
- Get size of image in SizeF object.
- Create an instance of Matrix class and perform composite transformation.
- Assign transformation to Graphics object.
- Create and initialize a StringFormat object.
- Draw a string as watermark using the Graphics class DrawString method.
- Save resultant image.
The following code snippet shows you how to add a diagonal watermark.
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()); | |
} |