.NET生成水印更好的方法實(shí)例代碼
前言
眾所周知為了保護(hù)知識(shí)產(chǎn)權(quán),防止資源被盜用,水印在博客、網(wǎng)店等場景中非常常見。
本文首先演示了基于System.Drawing.Image做正常操作。然后基于Direct2D/WIC/DirectWrite,演示了一種全新、不同的“騷”操作。
方法1-System.Drawing給圖片加水印
System.Drawing.Image原生屬于GDI的一部分,是Windows Only,但隨著NuGet包System.Drawing.Common的發(fā)布,現(xiàn)在System.Drawing.Image已經(jīng)支持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做了一個(gè)NuGet包,可以輕松地配置水印參數(shù):
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的。
Direct2D是Windows下一代的2D渲染庫,隨著Direct2D一起發(fā)布的,還有Windows Imaging Component(簡稱WIC)和DirectWrite。
相關(guān)說明和文檔鏈接:
| 技術(shù) | 說明 | 鏈接 |
|---|---|---|
| Direct2D | 基于硬件加速的2D圖形渲染 | Go |
| WIC | 高性能圖片編碼、解碼 | Go |
| DirectWrite | 基于硬件加速的文字渲染 | Go |
如果您打開鏈接看了一眼,就不難看出,這些技術(shù)都是基于COM的,但我們使用.NET,不是嗎?
好在我們有SharpDX
SharpDX對(duì)這些DirectX技術(shù)做了封裝,在這個(gè)Demo中,我們需要安裝SharpDX.Direct2D1和SharpDX.Mathematics兩個(gè)包:
Install-Package SharpDX.Direct2D1 -Version 4.2.0 Install-Package SharpDX.Mathematics -Version 4.2.0
以下代碼演示了如何使用SharpDX.Direct2D1給圖片加水?。?/p>
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;
}
}
}
調(diào)用方式:
File.WriteAllBytes(@"D:\_\Demo2.png", AddWatermark(File.OpenRead(@"D:\_\WatermarkDemo.png"), "水印在此").ToArray());
效果也是一切正常:

有什么區(qū)別?
System.Drawing只花了14行,Direct2D卻需要整整60行!復(fù)雜程度驚人!為什么要舍簡單求復(fù)雜呢?
因?yàn)镾ystem.Drawing沒有硬件加速,而且生成的圖片也沒有反走樣(Anti-aliasing),這導(dǎo)致使用System.Drawing相比之下較慢,而且生成圖片的效果稍差:

很明顯可以看出,Direct2D生成的圖片更平滑。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
- C#(.net)水印圖片的生成完整實(shí)例
- Asp.net 文件上傳類(取得文件后綴名,保存文件,加入文字水印)
- asp.net下用Aspose.Words for .NET動(dòng)態(tài)生成word文檔中的圖片或水印的方法
- asp.net 添加水印的代碼(已測試)
- .net生成縮略圖及水印圖片時(shí)出現(xiàn)GDI+中發(fā)生一般性錯(cuò)誤解決方法
- asp.net文件上傳功能(單文件,多文件,自定義生成縮略圖,水印)
- asp.net下GDI+的一些常用應(yīng)用(水印,文字,圓角處理)技巧
- .net c# gif動(dòng)畫如何添加圖片水印實(shí)現(xiàn)思路及代碼
- asp.net如何在圖片上加水印文字具體實(shí)現(xiàn)
- Asp.net簡單實(shí)現(xiàn)給圖片增加文字水印
相關(guān)文章
.net?core?api接口JWT方式認(rèn)證Token
本文詳細(xì)講解了.net?core?api接口JWT方式認(rèn)證Token,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
.NET?Core使用Worker?Service創(chuàng)建服務(wù)
這篇文章介紹了.NET?Core使用Worker?Service創(chuàng)建服務(wù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
.NET性能優(yōu)化之為集合類型設(shè)置初始大小的方法
這篇文章主要介紹了.NET性能優(yōu)化之為集合類型設(shè)置初始大小的方法,今天要談的一個(gè)性能優(yōu)化的Tips是一個(gè)老生常談的點(diǎn),但是也是很多人沒有注意的一個(gè)點(diǎn)。在使用集合類型是,你應(yīng)該設(shè)置一個(gè)預(yù)估的初始大小,那么為什么需要這樣做?我們一起來從源碼的角度說一說2022-05-05
ASP.NET WebAPi(selfhost)實(shí)現(xiàn)文件同步或異步上傳
這篇文章主要介紹了ASP.NET WebAPi(selfhost)實(shí)現(xiàn)文件同步或異步上傳,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Entity?Framework?Core基于數(shù)據(jù)模型創(chuàng)建數(shù)據(jù)庫
這篇文章介紹了Entity?Framework?Core基于數(shù)據(jù)模型創(chuàng)建數(shù)據(jù)庫的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
解決AJAX.NET中的懸停panel在頁面加載時(shí)閃爍的問題
AJAX.NET的兩個(gè)懸停控件.分別是HoverMenuExtender和ModalPopupExtender.他們可以打造很好的懸停效果...通常,我都是用panel來作為懸停內(nèi)容的容器..2009-06-06

