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’ll 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:

  1. Load an image.
  2. Create and initialize a Graphics object.
  3. Create and initialize Font and SolidBrush objects.
  4. Draw a string as watermark using the Graphics class DrawString method.
  5. Save image to PNG.

The following code snippet shows you how to add watermark on the image.

// 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());
}

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:

  1. Load an image.
  2. Create and initialize a Graphics object.
  3. Create and initialize Font and SolidBrush objects.
  4. Get size of image in SizeF object.
  5. Create an instance of Matrix class and perform composite transformation.
  6. Assign transformation to Graphics object.
  7. Create and initialize a StringFormat object.
  8. Draw a string as watermark using the Graphics class DrawString method.
  9. Save resultant image.

The following code snippet shows you how to add a diagonal watermark.

// 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());
}