65.9K
CodeProject 正在变化。 阅读更多。
Home

使用免费成像 SDK 轻松实现水印

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2011 年 4 月 8 日

CPOL

4分钟阅读

viewsIcon

27298

a. 向图像添加水印是保护所有者权益的常见任务。通过本教程和 Atalasoft 提供的免费 .NET 图像 SDK,这是一个简单而高效的过程。

使用 Atalasoft DotImage PhotoFree 轻松添加水印

这里有一个简单的方法可以使用 Atalasoft 的免费 DotImage Photo SDK 将水印添加到您的图像中。

还没有 DotImage Photo Free 吗? 好吧,它是免费的。

访问 http://www.atalasoft.com/photofree 并按照说明获取它。

如果您发现自己需要更多功能,包括对 ASP.NET、OCR 引擎、多页 TIFF 和 PDF 支持以及更多功能的支持,DotImage 还有两个更强大的版本。

阅读更多内容,请访问 http://www.atalasoft.com/dotimage/editions

使用 DotImage Photo Free 添加水印

对于照片内容网站来说,一个常见的任务是需要提供带有水印的图像校样。例如,私人摄影师可能希望向客户提供校样,但可能希望呈现分辨率较低或标记过的图像,以阻止客户购买照片或制作照片的权利。 在 DotImage 中,这是一个简单的任务。

在本文中,我们将逐步介绍该过程,然后查看代码以实现它。 我们首先需要的是水印的灰度图像。 这可以使用 DotImage 或绘图程序来制作——实际的工具并不重要。 水印应具有两个特征——首先,任何应透明显示的内容应为纯白色,任何应标记图像的内容应为纯黑色。 对于我们的示例,我们将使用此图像

image001.gif

在本例中,我创建了一个带有斜面灰色文本的白色背景图像。

下一个任务是将此图像与我们的源图像混合。 可以通过多种不同的方式混合两个图像——DotImage 支持近三十种。 在本例中,我们需要一种称为“multiply(正片叠底)”的混合方法。 源图像中每个像素的每个通道都将乘以相应水印像素的值。 如果水印像素是黑色,则其值为零。 如果水印像素是白色,则其值为一。 这意味着水印中的白色像素将保持源图像不变,而水印中的黑色像素将使源图像变黑。 介于两者之间的所有内容都将使图像变暗。

最终结果如下

image001.png

在代码方面,我将从一个方法开始,将水印图像放置在另一个图像中的特定位置

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

这只是使用正片叠底混合将 top 放置在给定位置的 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;
}

在此代码中,我们找到适合图像居中的位置(即宽度和高度的平均值)。 然后我们复制源图像。 这是因为 PlaceWatermarkAt 会修改源图像,而我们可能不希望这样。 接下来,水印图像的 PixelFormat 需要与源图像匹配,因此如果需要,我们会获取更改后的副本。 最后,我们调用 PlaceWatermarkAt() 来印上水印。 您会注意到我们已将代码放在 try/finally 块中。 这是为了确保万一发生错误,我们会在完成后处理掉 matchedTop。 即使 .NET 是一个垃圾收集环境,图像也会占用大量空间,最好在完成后处理掉它们。

最后一个细节是,我使 Watermark 成为 AtalaImage 上的扩展方法。 这意味着为了放置水印,您可以直接从 AtalaImage 执行此操作

AtalaImage watermarked = source.Watermark(notice);

作为最后的细节,我们还可以编写另一个 Watermark 版本,该版本将水印图像平铺在源图像上。 它看起来与居中代码非常相似,只是我们在嵌套循环中调用 PlaceWatermarkAt

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;
}

最终结果正如我们所期望的那样

image002.png

我们可以看到,使用 DotImage 中的工具可以轻松完成诸如水印图像之类的实际任务。

整个类如下,供参考。

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);
                }
            }
        }
    }
}

就这样! 一种使用 Atalasoft 的免费 DotImage Photo SDK 向图像添加水印的简单方法。

如果您发现自己需要更多功能,包括对 ASP.NET、OCR 引擎、多页 TIFF 和 PDF 支持以及更多功能的支持,DotImage 还有两个更强大的版本。

阅读更多内容,请访问 http://www.atalasoft.com/dotimage/editions

© . All rights reserved.