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.

using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
// load an existing PNG with Image.Load
using (var image = Aspose.Imaging.Image.Load(@"template.png"))
{
// create and initialize an instance of Graphics class and Initialize an object of SizeF to store image Size
var graphics = new Aspose.Imaging.Graphics(image);
var size = graphics.Image.Size;
// create an instance of Font. Initialize it with Font Face, Size and Style
var font = new Aspose.Imaging.Font("Times New Roman", 20, Aspose.Imaging.FontStyle.Bold);
// create an instance of SolidBrush and set Color & Opacity
var brush = new Aspose.Imaging.Brushes.SolidBrush();
brush.Color = Aspose.Imaging.Color.Red;
brush.Opacity = 0;
// initialize an object of StringFormat class and set its various properties
var format = new Aspose.Imaging.StringFormat();
format.Alignment = Aspose.Imaging.StringAlignment.Center;
format.FormatFlags = Aspose.Imaging.StringFormatFlags.MeasureTrailingSpaces;
// draw the string on image
graphics.DrawString("CONFIDENTIAL", font, brush, 0, 0, format);
// save output to disc
image.Save(@"output.png");
}

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.

using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.FileFormats.Jpeg;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Load an existing JPG image
using (Image image = Image.Load(dataDir + "template.tiff"))
{
// Declare a String object with Watermark Text
string theString = "45 Degree Rotated Text";
// Create and initialize an instance of Graphics class and Initialize an object of SizeF to store image Size
Graphics graphics = new Graphics(image);
SizeF sz = graphics.Image.Size;
// Creates an instance of Font, initialize it with Font Face, Size and Style
Font font = new Font("Times New Roman", 20, FontStyle.Bold);
// Create an instance of SolidBrush and set its various properties
SolidBrush brush = new SolidBrush();
brush.Color = Color.Red;
brush.Opacity = 0;
// Initialize an object of StringFormat class and set its various properties
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
// Create an object of Matrix class for transformation
Matrix matrix = new Matrix();
// First a translation then a rotation
matrix.Translate(sz.Width / 2, sz.Height / 2);
matrix.Rotate(-45.0f);
// Set the Transformation through Matrix
graphics.Transform = matrix;
// Draw the string on Image Save output to disk
graphics.DrawString(theString, font, brush, 0, 0, format);
image.Save(dataDir + "result.jpg");
}
File.Delete(dataDir + "result.jpg");