Click here to Skip to main content
Click here to Skip to main content
Go to top

Easy Watermarking With a Free Imaging SDK

, 8 Apr 2011
a. Adding watermarks to images is a common task to protect an owner’s rights. With this tutorial and a free .NET imaging SDK from Atalasoft, this is a simple and efficient process.

Editorial Note

This article is in the Product Showcase section for our sponsors at CodeProject. These reviews are intended to provide you with information on products and services that we consider useful and of value to developers.

Easy Watermarking Using Atalasoft DotImage PhotoFree

Here’s a simple way to add watermarks to your images with Atalasoft’s free DotImage Photo SDK.

Don’t have DotImage Photo Free yet? Well, it’s free.

Go to http://www.atalasoft.com/photofree and follow the instructions there to get it.

There are two beefier editions of DotImage if you find yourself wanting more features including support for ASP.NET, OCR engines, multipage TIFF & PDF support, and a lot more.

Read more at http://www.atalasoft.com/dotimage/editions

Watermarking with DotImage Photo Free

A common task for photo content web sites is the need to deliver watermarked proofs of images. For example, a private photographer might want to make proofs available to a client, but might want to present an image that is either lower resolution or marked so as to discourage a client from purchasing a print or the rights to make prints. In DotImage, this is an easy task.

In this article, we’ll cover the process step by step and then look at the code to make it happen. The first thing we need is a grayscale image for the watermark. This can be made using DotImage or a paint program – the actual tool is not important. The watermark should have two characteristics – first, anything that should act transparently should be pure white and anything that should mark the image should be pure black. For our sample, we’ll use this image:

image001.gif

In this case, I created an image with bevel gray text on a white background.

The next task is to blend this image with our source. There are a number of different ways that two images can be blended – DotImage supports almost three dozen. In this case we want a blend method called multiply. Each of the channels of each pixel in the source image will get multiplied by the value of the appropriate watermark pixel. If the watermark pixel is black, its value will be zero. If the watermark is pixel is white, its value will be one. The means that white pixels in the watermark will leave the source image alone and black pixels in the watermark will make the source image black. Everything in between will darken the image.

The end result is this:

image001.png

In terms of code, I’m going to start with a method to place the watermark image at a particular location within another image:

private static void PlaceWatermarkAt(AtalaImage source, AtalaImage top, Point location)
{
    OverlayMergedCommand overlay = new OverlayMergedCommand(top, location,
                                                            MergeOption.Multiply, 0.0);
    overlay.Apply(source);
}

This simply places top over source at the given location using multiply blending.

Next we’d like to have code that centers the watermark over the source.

public static AtalaImage Watermark(this AtalaImage source, AtalaImage top)
{
    int xpos = (source.Width - top.Width) / 2;
    int ypos = (source.Height - top.Height) / 2;
    AtalaImage final = source.Clone() as AtalaImage;
    AtalaImage matchedTop = top.PixelFormat == final.PixelFormat ?
                            top : top.GetChangedPixelFormat(source.PixelFormat);
    try {
        PlaceWatermarkAt(final, matchedTop, new Point(xpos, ypos));
    }
    finally {
        if (matchedTop != top)
            matchedTop.Dispose();

    }
    return final;
}

In this code, we find the appropriate place to center the image (which is the average of the widths and heights). Then we make a copy of the source image. This is because PlaceWatermarkAt will modify the source and we might not want that. Next, the watermark image’s PixelFormat needs to match the source, so we get a changed copy if we need it. Finally, we call PlaceWatermarkAt() to imprint the watermark. You’ll notice that we have that code in a try/finally block. This is to make sure that in case an error happens, we will get dispose of matchedTop when we’re done. Even though .NET is a garbage-collected environment, images can take up a great deal of space and it’s best to dispose them when you’re done.

The final detail is that I made Watermark an extension method on AtalaImage. This means that in order to place the watermark, you can do that directly from an AtalaImage:

AtalaImage watermarked = source.Watermark(notice);

As a final detail, we can also write another version of Watermark that tiles the watermark image over the source. It looks remarkably similar to the centering code except that we call PlaceWatermarkAt in a nested loop:

public static AtalaImage WatermarkTiled(this AtalaImage source, AtalaImage top)
{
    AtalaImage final = source.Clone() as AtalaImage;
    AtalaImage matchedTop = top.PixelFormat == final.PixelFormat ?
                            top : top.GetChangedPixelFormat(source.PixelFormat);
    try
    {
        for (int y = 0; y < final.Height; y += matchedTop.Height)
        {
            for (int x = 0; x < final.Width; x += matchedTop.Width)
            {
                PlaceWatermarkAt(final, matchedTop, new Point(x, y));
            }
        }
    }
    finally
    {
        if (matchedTop != top)
            matchedTop.Dispose();
    }
    return final;
}

And the end result is what we would expect:

image002.png

We can see that practical tasks like watermarking images are easy to do using the tools in DotImage.

The entire class is below for reference.

using System.Drawing;
using Atalasoft.Imaging;
using Atalasoft.Imaging.ImageProcessing;
 
namespace PhotoWatermark
{
    public static class ExtensionMethods
    {
        public static AtalaImage Watermark(this AtalaImage source, AtalaImage top)
        {
            int xpos = (source.Width - top.Width) / 2;
            int ypos = (source.Height - top.Height) / 2;
            AtalaImage final = source.Clone() as AtalaImage;
            AtalaImage matchedTop = top.PixelFormat == final.PixelFormat ?
                                    top : top.GetChangedPixelFormat(source.PixelFormat);
            try {
                PlaceWatermarkAt(final, matchedTop, new Point(xpos, ypos));
            }
            finally {
                if (matchedTop != top)
                    matchedTop.Dispose();
            }
            return final;
        }
 
        public static AtalaImage WatermarkTiled(this AtalaImage source, AtalaImage top)
        {
            AtalaImage final = source.Clone() as AtalaImage;
            AtalaImage matchedTop = top.PixelFormat == final.PixelFormat ?
                                    top : top.GetChangedPixelFormat(source.PixelFormat);
            try
            {
                for (int y = 0; y < final.Height; y += matchedTop.Height)
                {
                    for (int x = 0; x < final.Width; x += matchedTop.Width)
                    {
                        PlaceWatermarkAt(final, matchedTop, new Point(x, y));
                    }
                }
            }
            finally
            {
                if (matchedTop != top)
                    matchedTop.Dispose();
            }
            return final;
        }
 
        private static void PlaceWatermarkAt(AtalaImage source,
                                             AtalaImage top, Point location)
        {
            OverlayMergedCommand overlay = new OverlayMergedCommand(top,
                                                   location, MergeOption.Multiply, 0.0);
            overlay.Apply(source);
        }
    }
}

using Atalasoft.Imaging;
using Atalasoft.Imaging.Codec;
 
namespace PhotoWatermark
{
    class Program
    {
        static void Main(string[] args)
        {
            using (AtalaImage source = new AtalaImage(@"..\..\Sabre.jpg", null),
                              notice = new AtalaImage(@"..\..\CopyRightNotice.png"))
            {
                using (AtalaImage watermarked = source.WatermarkTiled(notice))
                {
                    watermarked.Save("sabrewatermark.jpg", new JpegEncoder(), null);
                }
            }
        }
    }
}

There you go! A simple way to add watermarks to your images with Atalasoft’s free DotImage Photo SDK.

There are two beefier editions of DotImage if you find yourself wanting more features including support for ASP.NET, OCR engines, multipage TIFF & PDF support, and a lot more.

Read more at http://www.atalasoft.com/dotimage/editions

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Steve Hawley
Architect Atalasoft, a Kofax Company
United States United States
Steve Hawley is a software engineer/architect at Atalasoft, Inc., responsible for current and future component designs.
Follow on   Twitter

Comments and Discussions

 
SuggestionWhy buy... PinmemberPaw Jershauge15-Jul-11 10:52 
GeneralSlimmer download Pinmemberwmjordan20-May-11 22:04 
QuestionA library... for 10 lines of code? Pinmembertlhintoq11-Apr-11 13:31 
GeneralRe: A library... for 10 lines of code? PinmemberKalpana Volety11-Jan-13 5:53 
I should say, I agree with you.
However, thanks for the effort to make the library.
 
Prasad Gurla
Watermark PDF

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 8 Apr 2011
Article Copyright 2011 by Steve Hawley
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid