Add watermark to JPEG images

In this tutorial, you implement a file processor, called WatermarkMaker, that is executed each time you upload a JPEG image in your library. The file processor adds a watermark in the top left corner of every image you upload.

Original image:

original image

Processed image:

processed image

You first create the file processor class and then add the file processor in the administration settings in Sitefinity CMS backend.

Implement the file processor

Create a file processor class that inherits the FileProcessorBase class. Next, implement your logic for the CanProcessFile and Process methods:

C#
using System.Collections.Specialized;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Telerik.Sitefinity.FileProcessors;
using Telerik.Sitefinity.Processors;

namespace MyFileProcessors
{
   public class WatermarkMaker : ProcessorBase, IFileProcessor
   {
       private string font;
       private string text;
       private string color;

       protected override void Initialize(NameValueCollection config)
       {
           this.font = config["Font"] ?? "Arial";
           this.color = config["Color"] ?? "Black";
           this.text = config["Text"] ?? "Example txt";
       }

       public bool CanProcessFile(FileProcessorInput fileInput)
       {
           if (fileInput.MimeType == "image/jpeg")
           {
               return true;
           }

           return false;
       }

       public Stream Process(FileProcessorInput fileInput)
       {
           Stream output = new MemoryStream();

           // load the image
           Image img = Image.FromStream(fileInput.FileStream);

           // set font
           Font font = new Font(this.font, 20, FontStyle.Bold, GraphicsUnit.Pixel);

           // set color
           Color color = Color.FromName(this.color);
           Point pt = new Point(5, 5);
           SolidBrush brush = new SolidBrush(color);

           // draw watermark text
           Graphics graphics = Graphics.FromImage(img);
           graphics.DrawString(this.text, font, brush, pt);
           graphics.Dispose();

           // save the image into a stream
           img.Save(output, ImageFormat.Jpeg);
           img.Dispose();
           output.Position = 0;

           return output;
       }
   }
}

NOTE: In the code above, you also override the Initialize(NameValueCollection config) method. The config parameter, passed to the method, lists all additional parameters that the file processor uses. In this example, font, color, and text.

Add the file processor as administration setting

  1. In Sitefinity CMS backend, navigate to Administration » Settings » Advanced settings.
  2. Expand the Libraries node and click File processors.
    Click Create new.
  3. Enter the following:
  • Name: WatermarkMaker

  • Description: Watermark maker for JPEG images

  • Type: MyFileProcessors.WatermarkMaker

    NOTE:The type must be identical to the name of the class you implemented in the previous section of the tutorial.

  1. Save your changes.
  2. To add parameters to the WatermarkMaker processor, expand its node and for each of the parameters, enter the following values, and save your changes:

    Parameter 

    Value

    Text 

    Example 

    Font 

    Arial 

    Color 

    Red

    As a result, all images of MIME type “image/jpeg”, for example, JPEG, JPG, and JPE are processed by the WatermarkMaker file processor before being saved in the library.

Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.