最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

.NET6使用ImageSharp實現(xiàn)給圖片添加水印

 更新時間:2022年12月01日 08:16:12   作者:gmval  
這篇文章主要為大家詳細介紹了.NET6使用ImageSharp實現(xiàn)給圖片添加水印功能的相關(guā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)文章

  • .net 通過URL推送POST數(shù)據(jù)具體實現(xiàn)

    .net 通過URL推送POST數(shù)據(jù)具體實現(xiàn)

    這篇文章主要介紹了.net 通過URL推送POST數(shù)據(jù)具體實現(xiàn),有需要的朋友可以參考一下
    2013-12-12
  • WPF中自定義GridLengthAnimation

    WPF中自定義GridLengthAnimation

    這篇文章主要為大家詳細介紹了WPF中自定義GridLengthAnimation的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Asp.net MVC中Razor常見的問題與解決方法總結(jié)

    Asp.net MVC中Razor常見的問題與解決方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于Asp.net MVC中Razor常見的問題與解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • ASP.net與SQLite數(shù)據(jù)庫通過js和ashx交互(連接和操作)

    ASP.net與SQLite數(shù)據(jù)庫通過js和ashx交互(連接和操作)

    這篇文章主要介紹了ASP.net與SQLite數(shù)據(jù)庫通過js和ashx交互(連接和操作),具有一定的參考價值,有興趣的可以了解一下。
    2017-01-01
  • Entity?Framework代碼優(yōu)先Code?First入門

    Entity?Framework代碼優(yōu)先Code?First入門

    這篇文章介紹了Entity?Framework的代碼優(yōu)先模式Code?First,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • asp.net core 獲取 MacAddress 地址方法示例

    asp.net core 獲取 MacAddress 地址方法示例

    這篇文章主要介紹了asp.net core獲取MacAddress地址方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • asp.net批量多選文件上傳解決方案

    asp.net批量多選文件上傳解決方案

    這篇文章主要介紹了asp.net批量多選文件上傳解決方案,基于flex開發(fā)的一個多選上傳功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2015-08-08
  • Asp.Net Core中創(chuàng)建多DbContext并遷移到數(shù)據(jù)庫的步驟

    Asp.Net Core中創(chuàng)建多DbContext并遷移到數(shù)據(jù)庫的步驟

    這篇文章主要介紹了Asp.Net Core中創(chuàng)建多DbContext并遷移到數(shù)據(jù)庫的步驟,幫助大家更好的理解和學(xué)習(xí)使用Asp.Net Core,感興趣的朋友可以了解下
    2021-03-03
  • 部署.NET6項目到IIS

    部署.NET6項目到IIS

    這篇文章介紹了部署.NET6項目到IIS的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • ASP.NET MVC自定義錯誤頁面真的簡單嗎?

    ASP.NET MVC自定義錯誤頁面真的簡單嗎?

    ASP.NET MVC自定義錯誤頁面真的簡單嗎?這篇文章主要介紹了ASP.NET MVC自定義錯誤頁面,感興趣的小伙伴們可以參考一下
    2016-10-10

最新評論

大荔县| 定襄县| 兰西县| 恩施市| 玛曲县| 襄樊市| 阿尔山市| 古蔺县| 四平市| 江达县| 灌云县| 青铜峡市| 富民县| 临汾市| 桦川县| 深圳市| 内黄县| 通化县| 奈曼旗| 额尔古纳市| 孟州市| 新民市| 云林县| 台南县| 日土县| 保亭| 呈贡县| 和林格尔县| 铁力市| 和田市| 盖州市| 衡水市| 大兴区| 阿鲁科尔沁旗| 乐清市| 景东| 巴彦县| 菏泽市| 莱州市| 彭山县| 亳州市|