Add watermark to images using C#

Watermarks play a vital role in asserting ownership of images and preventing unauthorized usage or counterfeiting. Implementing an automated watermarking feature in your web or desktop applications can significantly enhance your image security. In this article, we will guide you through the process of adding watermarks to images programmatically using C#. Additionally, you’ll learn how to add a watermark to PDF files using C# to further secure your documents. Let’s dive in!

Table of Contents

C# API to Add Watermark to Images

To add watermarks to images, we will utilize the Aspose.Imaging for .NET. This powerful image processing API supports various formats, including PNG, JPEG, and BMP, making image manipulation straightforward. You can either download the API binaries or install it via NuGet using the following command:

PM> Install-Package Aspose.Imaging

Add Watermark to an Image in C#

Follow these steps to add a watermark to an image using C#:

  1. Load the image using the Image class.
  2. Create an instance of the Graphics class, initializing it with the Image object.
  3. Define the font family, size, and style using the Font class.
  4. Create a SolidBrush instance to set properties like color.
  5. Instantiate the StringFormat class to manage text alignment.
  6. Add the watermark to the image using the Graphics.DrawString(String, Font, SolidBrush, 0, 0, StringFormat) method.
  7. Save the image using the Image.Save(String) method.

Here’s a code sample demonstrating how to add a watermark to an image in C#:

Add watermark to image using C#

C# Add Diagonal Watermark to Images

Sometimes, watermarks are applied diagonally across images for a more distinctive effect. The Aspose.Imaging for .NET API allows you to rotate the watermark text to achieve this. Here’s how to add a diagonal watermark:

  1. Load the image using the Image class.
  2. Create a Graphics instance initialized with the Image object.
  3. Define the font family, size, and style with the Font class.
  4. Create a SolidBrush instance to set properties like color.
  5. Instantiate a StringFormat object for text alignment.
  6. Create a Matrix instance to set the transformation angle.
  7. Assign the Matrix object to the Graphics.Transform property.
  8. Add the watermark using the Graphics.DrawString(String, Font, SolidBrush, 0, 0, StringFormat) method.
  9. Save the image using the Image.Save(String) method.

Here’s a code sample that illustrates how to add a diagonal watermark to images using C#:

Add diagonal watermark to image using C#

Insert Watermark on Images with a Free License

You can get a free temporary license to insert watermarks on images without any evaluation limitations.

Conclusion

In this article, you learned how to add a watermark to images in C#. You also discovered how to rotate watermark text and how to add a watermark in PDF using C#. For further information about the C# image processing API, please refer to the documentation. If you have any questions or need assistance, feel free to reach out via our forum.

See Also

More in this category