C# API to Add Watermark on Photos or Images

Add watermark to your photos (e.g. JPG, GIF, TIFF, PNG or BMP images) is actually adding a more or less visible information in the form of a text or some other image to real photos. The watermark changes the image on the pixel level and the pixels that make up the resulting watermark is changed more or less in the direction of the watermarking image

GroupDocs.Watermark for adding watermarks on images or photos

The GroupDocs.Watermark is an API for adding watermarks to the images or the documents of different file formats. It provides the effective watermarking methods that allow you to add watermarks that are hard to be automatically removed by third-party tools.

Lets discuss some use cases that how easily we can add the watermarks to images by writing code using GroupDocs.Watermark for .NET API.

Adding watermark to multi-framed image

When you are working with an animated gif or multi-frame tiff images, you may want to add watermark to some particular frame(s). 

The implementation of this use case is given below:

TiffImageLoadOptions loadOptions = new TiffImageLoadOptions();
// Constants.InImageTiff is an absolute or relative path to your document. Ex: @"C:\Docs\image.tiff"
using (Watermarker watermarker = new Watermarker(Constants.InImageTiff, loadOptions))
{
    // Initialize text or image watermark
    TextWatermark watermark = new TextWatermark("Test watermark", new Font("Arial", 19));
 
    // Add watermark to the first frame
    TiffImageWatermarkOptions options = new TiffImageWatermarkOptions();
    options.FrameIndex = 0;
 
    watermarker.Add(watermark, options);
    watermarker.Save(Constants.OutImageTiff);
}

You can see the black watermark at the top left corner of the image, like the screenshot given below:

Adding watermark to images inside a document

The most of the document formats allow you to place images inside a document. If you want to add watermark to images inside a document, then it can be possible using GroupDocs.Watermark. Following are the steps to add watermark to the images of any document.

  1. Load the document 
  2. Create and initialize watermark object 
  3. Set watermark properties 
  4. Find images in the document
  5. Add watermark to all found images
  6. Save the document

The following code snippet shows the implementation:

// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"
using (Watermarker watermarker = new Watermarker(Constants.InDocumentPdf))
{
    // Initialize text watermark
    TextWatermark textWatermark = new TextWatermark("Protected image", new Font("Arial", 8));
    textWatermark.HorizontalAlignment = HorizontalAlignment.Center;
    textWatermark.VerticalAlignment = VerticalAlignment.Center;
    textWatermark.RotateAngle = 45;
    textWatermark.SizingType = SizingType.ScaleToParentDimensions;
    textWatermark.ScaleFactor = 1;
 
    // Initialize image watermark
    using (ImageWatermark imageWatermark = new ImageWatermark(Constants.ProtectJpg))
    {
        imageWatermark.HorizontalAlignment = HorizontalAlignment.Center;
        imageWatermark.VerticalAlignment = VerticalAlignment.Center;
        imageWatermark.RotateAngle = -45;
        imageWatermark.SizingType = SizingType.ScaleToParentDimensions;
        imageWatermark.ScaleFactor = 1;
 
        // Find all images in a document
        WatermarkableImageCollection images = watermarker.GetImages();
        for (int i = 0; i < images.Count; i++)
        {
            if (images[i].Width > 100 && images[i].Height > 100)
            {
                if (i % 2 == 0)
                {
                    images[i].Add(textWatermark);
                }
                else
                {
                    images[i].Add(imageWatermark);
                }
            }
        }
    }
 
    watermarker.Save(Constants.OutDocumentPdf);
}

The complete ready to run code sample is available on GitHub.

Share on FacebookTweet about this on TwitterShare on LinkedIn