1. 程式人生 > >.NET中生成水印更好的方法

.NET中生成水印更好的方法

.NET中生成水印更好的方法

為了保護智慧財產權,防止資源被盜用,水印在部落格、網店等場景中非常常見。

本文首先演示了基於System.Drawing.Image做正常操作。然後基於Direct2D/WIC/DirectWrite,演示了一種全新、不同的“騷”操作。

方法1-System.Drawing給圖片加水印

System.Drawing.Image原生屬於GDI的一部分,是Windows Only,但隨著NuGet包System.Drawing.Common的釋出,現在System.Drawing.Image已經支援linux

Install-Package System.Drawing.Common -Version 4.5.1

以下程式碼演示瞭如何從給圖片加水印:

// 加水印
var watermarkedStream = new MemoryStream();
using (var img = Image.FromStream(File.OpenRead(@"D:\_\WatermarkDemo.png")))
{
    using (var graphic = Graphics.FromImage(img))
    {
        var font = new Font("微軟雅黑", 30, FontStyle.Bold, GraphicsUnit.Pixel);
        var color = Color.FromArgb(128, 255, 255, 255);
        var brush = new SolidBrush(color);
        var point = new Point(img.Width - 130, img.Height - 50);

        graphic.DrawString("水印在此", font, brush, point);
        img.Save(watermarkedStream, ImageFormat.Png);
    }
}

效果如圖(沒有黃色剪頭):

附:Edi.Wang做了一個NuGet包,可以輕鬆地配置水印引數:

  • NuGet:https://github.com/EdiWang/Edi.ImageWatermark
  • 文章:https://edi.wang/post/2018/10/12/add-watermark-to-uploaded-image-aspnet-core

方法2-Direct2D/WIC給圖片加水印

Direct2D源於Windows 8/IE 10,安裝IE 10之後,Windows 7也能用。Direct2D基於Direct3D,很顯然,是Windows Only的。

Direct2DWindows下一代的2D渲染庫,隨著Direct2D

一起釋出的,還有Windows Imaging Component(簡稱WIC)和DirectWrite

相關說明和文件連結:

技術 說明 連結
Direct2D 基於硬體加速的2D圖形渲染 Go
WIC 高效能圖片編碼、解碼 Go
DirectWrite 基於硬體加速的文字渲染 Go

如果您開啟連結看了一眼,就不難看出,這些技術都是基於COM的,但我們使用.NET,不是嗎?

好在我們有SharpDX

SharpDX對這些DirectX技術做了封裝,在這個Demo中,我們需要安裝SharpDX.Direct2D1SharpDX.Mathematics兩個包:

Install-Package SharpDX.Direct2D1 -Version 4.2.0
Install-Package SharpDX.Mathematics -Version 4.2.0

以下程式碼演示瞭如何使用SharpDX.Direct2D1給圖片加水印:

using D2D = SharpDX.Direct2D1;
using DWrite = SharpDX.DirectWrite;
using SharpDX;
using SharpDX.IO;
using WIC = SharpDX.WIC;

MemoryStream AddWatermark(Stream fileName, string watermarkText)
{
    using (var wic = new WIC.ImagingFactory2())
    using (var d2d = new D2D.Factory())
    using (var image = CreateWicImage(wic, fileName))
    using (var wicBitmap = new WIC.Bitmap(wic, image.Size.Width, image.Size.Height, WIC.PixelFormat.Format32bppPBGRA, WIC.BitmapCreateCacheOption.CacheOnDemand))
    using (var target = new D2D.WicRenderTarget(d2d, wicBitmap, new D2D.RenderTargetProperties()))
    using (var bmpPicture = D2D.Bitmap.FromWicBitmap(target, image))
    using (var dwriteFactory = new SharpDX.DirectWrite.Factory())
    using (var brush = new D2D.SolidColorBrush(target, new Color(0xff, 0xff, 0xff, 0x7f)))
    {
        target.BeginDraw();
        {
            target.DrawBitmap(bmpPicture, new RectangleF(0, 0, target.Size.Width, target.Size.Height), 1.0f, D2D.BitmapInterpolationMode.Linear);
            target.DrawRectangle(new RectangleF(0, 0, target.Size.Width, target.Size.Height), brush);
            var textFormat = new DWrite.TextFormat(dwriteFactory, "微軟雅黑", DWrite.FontWeight.Bold, DWrite.FontStyle.Normal, 30.0f);
            target.DrawText(watermarkText, textFormat, new RectangleF(target.Size.Width - 130, target.Size.Height - 50, int.MaxValue, int.MaxValue), brush);
        }
        target.EndDraw();

        var ms = new MemoryStream();
        SaveD2DBitmap(wic, wicBitmap, ms);
        return ms;
    }
}

void SaveD2DBitmap(WIC.ImagingFactory wicFactory, WIC.Bitmap wicBitmap, Stream outputStream)
{
    using (var encoder = new WIC.BitmapEncoder(wicFactory, WIC.ContainerFormatGuids.Png))
    {
        encoder.Initialize(outputStream);
        using (var frame = new WIC.BitmapFrameEncode(encoder))
        {
            frame.Initialize();
            frame.SetSize(wicBitmap.Size.Width, wicBitmap.Size.Height);

            var pixelFormat = wicBitmap.PixelFormat;
            frame.SetPixelFormat(ref pixelFormat);
            frame.WriteSource(wicBitmap);

            frame.Commit();
            encoder.Commit();
        }
    }
}

WIC.FormatConverter CreateWicImage(WIC.ImagingFactory wicFactory, Stream stream)
{
    using (var decoder = new WIC.PngBitmapDecoder(wicFactory))
    {
        var decodeStream = new WIC.WICStream(wicFactory, stream);
        decoder.Initialize(decodeStream, WIC.DecodeOptions.CacheOnLoad);
        using (var decodeFrame = decoder.GetFrame(0))
        {
            var converter = new WIC.FormatConverter(wicFactory);
            converter.Initialize(decodeFrame, WIC.PixelFormat.Format32bppPBGRA);
            return converter;
        }
    }
}

呼叫方式:

File.WriteAllBytes(@"D:\_\Demo2.png", AddWatermark(File.OpenRead(@"D:\_\WatermarkDemo.png"), "水印在此").ToArray());

效果也是一切正常:

有什麼區別?

System.Drawing只花了14行,Direct2D卻需要整整60行!複雜程度驚人!為什麼要舍簡單求複雜呢?

因為System.Drawing沒有硬體加速,而且生成的圖片也沒有反走樣(Anti-aliasing),這導致使用System.Drawing相比之下較慢,而且生成圖片的效果稍差:

很明顯可以看出,Direct2D生成的圖片更平滑。


作者:周杰
出處:https://www.cnblogs.com/sdflysha
本文采用 知識共享署名-非商業性使用-相同方式共享 2.5 中國大陸許可協議 進行許可,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線