.NET6使用ImageSharp實現(xiàn)給圖片添加水印
.NET 6 中,使用System.Drawing操作圖片,生成解決方案或打包的時候,會有警告,意思是System.Drawing僅在 'windows' 上受支持。微軟官方的解釋是:
System.Drawing.Common NuGet 包現(xiàn)在被歸為 Windows 特定的庫。 在為非 Windows 操作系統(tǒng)編譯時,平臺分析器會在編譯時發(fā)出警告。
在非 Windows 操作系統(tǒng)上,除非設(shè)置了運行時配置開關(guān),否則將引發(fā) TypeInitializationException 異常,其中 PlatformNotSupportedException 作為內(nèi)部異常
在 .NET 6 之前,使用 System.Drawing.Common 包不會產(chǎn)生任何編譯時警告,也不會引發(fā)任何運行時異常。
從 .NET 6 開始,當(dāng)為非 Windows 操作系統(tǒng)編譯引用代碼時,平臺分析器會發(fā)出編譯時警告。
當(dāng)然,使用windows操作系統(tǒng)沒有任何問題,Linux的話,需要單獨的配置。
可以通過在runtimeconfig.json文件中將System.Drawing.EnableUnixSupport 運行時配置開關(guān)設(shè)置為來啟用對 .NET 6 中的非 Windows 平臺的支持:true
或者使用第三方庫
- ImageSharp
- SkiaSharp
- Microsoft.Maui.Graphics
正如標(biāo)題,我使用了ImageSharp來操作圖片,并給圖片添加水印
//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; }
代碼比較簡單,首先使用SixLabors.ImageSharp.Image.LoadAsync打開圖片,然后使用ImageSharp的自定義擴展方法給圖片添加水印。
ApplyScalingImageWaterMark擴展方法:
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) //對水印圖片進行縮放 { if (imgSize.Width > imgSize.Height)//橫的長方形 { 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)); } //水印圖片完成成立,開始根據(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類:
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實現(xiàn)給圖片添加水印的文章就介紹到這了,更多相關(guān).NET ImageSharp圖片添加水印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解ASP.NET?Core高性能服務(wù)器HTTP.SYS
HTTP.SYS本質(zhì)上就是一個HTTP/HTTPS監(jiān)聽器,它是Windows網(wǎng)絡(luò)子系統(tǒng)的一部分,是一個在內(nèi)核模式下運行的網(wǎng)絡(luò)驅(qū)動,今天通過本文給大家介紹下ASP.NET?Core高性能服務(wù)器HTTP.SYS,感興趣的朋友一起看看吧2022-04-04如何利用HttpClientFactory實現(xiàn)簡單的熔斷降級
這篇文章主要給大家介紹了關(guān)于如何利用HttpClientFactory實現(xiàn)簡單的熔斷降級的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07.NET?6?中的?dotnet?monitor詳細(xì)解析
dotnet?monitor?是一種工具,它提供了一種統(tǒng)一的方法來收集這些診斷信息,而不管您是在桌面計算機還是在?kubernetes?集群中運行,dotnet?monitor?已經(jīng)為?Azure?App?Service(Linux)提供?.NET?應(yīng)用程序的診斷工具提供支持,我們希望它在更多環(huán)境中使用2021-12-12.Net微信網(wǎng)頁開發(fā)解決用戶在不同公眾號或在公眾號、移動應(yīng)用之間帳號統(tǒng)一問題
這篇文章主要介紹了.Net微信網(wǎng)頁開發(fā)解決用戶在不同公眾號或在公眾號、移動應(yīng)用之間帳號統(tǒng)一問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09xls表格導(dǎo)入數(shù)據(jù)庫功能實例代碼
這篇文章介紹了xls表格導(dǎo)入數(shù)據(jù)庫功能實例代碼,有需要的朋友可以參考一下2013-10-10.NET集成DeveloperSharp實現(xiàn)高效分頁與無主鍵分頁
DeveloperSharp系列近期又被制造業(yè)ERP、民航飛行App、建筑BIM、電力掌上營業(yè)廳、等多家大型采用,站在巨人的肩膀上你能走的更遠,這篇文章主要介紹了.NET集成DeveloperSharp實現(xiàn)高效分頁與無主鍵分頁,需要的朋友可以參考下2024-06-06基于?.NET?6?的ASP.NET?Core啟動地址配置方法及優(yōu)先級順序
這篇文章主要介紹了ASP.NET?Core啟動地址配置方法及優(yōu)先級順序,?.NET?6?使用了最小?WEB?API,?配置方式已經(jīng)部分發(fā)生了變化,下面文章我們來看看具體的方法,需要的小伙伴可以參考一下2022-03-03