使用.NET怎么生成水印-创新互联
这篇文章将为大家详细讲解有关使用.NET怎么生成水印,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
我们提供的服务有:网站设计、网站制作、微信公众号开发、网站优化、网站认证、潮南ssl等。为成百上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的潮南网站制作公司方法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的。
Direct2D是Windows下一代的2D渲染库,随着Direct2D一起发布的,还有Windows Imaging Component(简称WIC)和DirectWrite。
相关说明和文档链接:
技术 | 说明 | 链接 |
---|---|---|
Direct2D | 基于硬件加速的2D图形渲染 | Go |
WIC | 高性能图片编码、解码 | Go |
DirectWrite | 基于硬件加速的文字渲染 | Go |
如果您打开链接看了一眼,就不难看出,这些技术都是基于COM的,但我们使用.NET,不是吗?
好在我们有SharpDX
SharpDX对这些DirectX技术做了封装,在这个Demo中,我们需要安装SharpDX.Direct2D1和SharpDX.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());
效果也是一切正常:
有什么区别?
关于使用.NET怎么生成水印就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
当前名称:使用.NET怎么生成水印-创新互联
标题来源:http://ybzwz.com/article/diccdi.html