.NET6使用ImageSharp實(shí)現(xiàn)給圖片添加水印
.NET 6 中,使用System.Drawing操作圖片,生成解決方案或打包的時(shí)候,會(huì)有警告,意思是System.Drawing僅在 'windows' 上受支持。微軟官方的解釋是:
System.Drawing.Common NuGet 包現(xiàn)在被歸為 Windows 特定的庫(kù)。 在為非 Windows 操作系統(tǒng)編譯時(shí),平臺(tái)分析器會(huì)在編譯時(shí)發(fā)出警告。
在非 Windows 操作系統(tǒng)上,除非設(shè)置了運(yùn)行時(shí)配置開(kāi)關(guān),否則將引發(fā) TypeInitializationException 異常,其中 PlatformNotSupportedException 作為內(nèi)部異常
在 .NET 6 之前,使用 System.Drawing.Common 包不會(huì)產(chǎn)生任何編譯時(shí)警告,也不會(huì)引發(fā)任何運(yùn)行時(shí)異常。
從 .NET 6 開(kāi)始,當(dāng)為非 Windows 操作系統(tǒng)編譯引用代碼時(shí),平臺(tái)分析器會(huì)發(fā)出編譯時(shí)警告。
當(dāng)然,使用windows操作系統(tǒng)沒(méi)有任何問(wèn)題,Linux的話(huà),需要單獨(dú)的配置。
可以通過(guò)在runtimeconfig.json文件中將System.Drawing.EnableUnixSupport 運(yùn)行時(shí)配置開(kāi)關(guān)設(shè)置為來(lái)啟用對(duì) .NET 6 中的非 Windows 平臺(tái)的支持:true
或者使用第三方庫(kù)
- ImageSharp
- SkiaSharp
- Microsoft.Maui.Graphics
正如標(biāo)題,我使用了ImageSharp來(lái)操作圖片,并給圖片添加水印
//ImageFile為圖片物理路徑,如下方的注釋 public async Task<ImageResult> WaterMark(string ImageFile) { ImageResult result = new ImageResult(); //var ImageFile = "D:\www\wwwroot\upload\5176caebc1404caa8b0b350181ae28ab.jpg"; var WaterMark = "D:\\www\\wwwroot\\watermark.png"; string FileName = Guid.NewGuid().ToString("N") + ".jpg"; string SavePath = "D:\\www\\wwwrootupload\\" + FileName; string imgurl = "/upload/"+FileName; //為了與System.Drawing.Common有所區(qū)別,引用使用全路徑 using (var image = await SixLabors.ImageSharp.Image.LoadAsync(ImageFile)) { using (var clone = image.Clone(ctx => ctx.ApplyScalingImageWaterMark("center"))) { await clone.SaveAsync(SavePath); } result.width = image.Width; result.height = image.Height; result.url = imgurl; result.format = ".jpg"; result.state = true; } return result; }
代碼比較簡(jiǎn)單,首先使用SixLabors.ImageSharp.Image.LoadAsync打開(kāi)圖片,然后使用ImageSharp的自定義擴(kuò)展方法給圖片添加水印。
ApplyScalingImageWaterMark擴(kuò)展方法:
public static class ImageSharpExtention { public static IImageProcessingContext ApplyScalingImageWaterMark(this IImageProcessingContext processingContext, string waterPosition = "center",string waterPath) { using (var mark_image = SixLabors.ImageSharp.Image.Load(waterPath)) { int markWidth = mark_image.Width; int markHeight = mark_image.Height; var imgSize = processingContext.GetCurrentSize(); if (markWidth >= imgSize.Width || markHeight >= imgSize.Height) //對(duì)水印圖片進(jìn)行縮放 { if (imgSize.Width > imgSize.Height)//橫的長(zhǎng)方形 { markWidth = imgSize.Width / 2; //寬縮放一半 markHeight = (markWidth * imgSize.Height) / imgSize.Width; } else { markHeight = imgSize.Height / 2; markWidth = (markHeight * imgSize.Width) / imgSize.Height; } mark_image.Mutate(mk => mk.Resize(markWidth, markHeight)); } //水印圖片完成成立,開(kāi)始根據(jù)位置添加水印 var position = waterPosition; if (string.IsNullOrEmpty(position)) { position = "center"; } position = position.ToLower(); if (string.IsNullOrEmpty(position)) { position = "center"; } SixLabors.ImageSharp.Point point = new SixLabors.ImageSharp.Point(); //左上 if (position.Contains("lefttop")) { point.X = 10; point.Y = 10; } //上中 if (position.Contains("topcenter")) { point.X = (imgSize.Width - mark_image.Width) / 2; point.Y = 10; } //右上 if (position.Contains("righttop")) { point.X = (imgSize.Width - mark_image.Width) - 10; point.Y = 10; } //右中 if (position.Contains("rightcenter")) { point.X = (imgSize.Width - mark_image.Width) - 10; point.Y = (imgSize.Height - mark_image.Height) / 2; } //右下 if (position.Contains("rightbottom")) { point.X = (imgSize.Width - mark_image.Width) - 10; point.Y = (imgSize.Height - mark_image.Height) - 10; } //下中 if (position.Contains("bottomcenter")) { point.X = (imgSize.Width - mark_image.Width) / 2; point.Y = (imgSize.Height - mark_image.Height) - 10; } //左下 if (position.Contains("leftbottom")) { point.X = 10; point.Y = (imgSize.Height - mark_image.Height) - 10; } //左中 if (position.Contains("leftcenter")) { point.X = 10; point.Y = (imgSize.Height - mark_image.Height) / 2; } if (position.Contains("center")) { point.X = (imgSize.Width - mark_image.Width) / 2; point.Y = (imgSize.Height - mark_image.Height) / 2; } float opacity=(float)0.8;//設(shè)置不透明度,0-1之間 //添加水印 return processingContext.DrawImage(mark_image,point,opacity); } } }
ImageResult類(lèi):
public class ImageResult { /// <summary> /// 文件名 /// </summary> public string id { get; set; } /// <summary> /// 文件大小 /// </summary> public string size { get; set; } /// <summary> /// 文件路徑 /// </summary> public string url { get; set; } /// <summary> /// 文件格式 /// </summary> public string format { get; set; } /// <summary> /// 上傳狀態(tài) /// </summary> public bool state { get; set; } /// <summary> /// 上傳消息 /// </summary> public string msg { get; set; } /// <summary> /// 圖片寬 /// </summary> public int width { get; set; } /// <summary> /// 圖片高 /// </summary> public int height { get; set; } }
到此這篇關(guān)于.NET6使用ImageSharp實(shí)現(xiàn)給圖片添加水印的文章就介紹到這了,更多相關(guān).NET ImageSharp圖片添加水印內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
