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.Imaging. 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.

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.