色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:225日期:2022-06-09 09:21:59

.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

正如標題,我使用了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)文章希望大家以后多多支持!

標簽: ASP.NET
主站蜘蛛池模板: 亚洲人的天堂男人爽爽爽 | 亚洲国产激情一区二区三区 | 久久久久综合给合狠狠狠 | 在线视频日韩 | 欧美精品做人一级爱免费 | 久久精品亚洲乱码伦伦中文 | 日本一级大黄毛片免费基地 | 美女福利视频国产片 | 日韩精品免费一级视频 | 99成人免费视频 | 日韩专区亚洲精品欧美专区 | 欧美一级毛片免费播放器 | 日本精品一区二区三区在线视频 | 欧美成人综合 | 亚洲国产精品久久久久秋霞不卡 | 国产最新精品 | 久久久免费观看 | 免费观看一级欧美大 | 亚洲国产精品一区二区三区 | 步兵网站| 手机看片日韩国产一区二区 | 欧美free性秘书xxxxhd | 国产一级网站 | 久久成人18免费 | 亚洲综合日韩精品欧美综合区 | 一个人看的免费观看日本视频www | 欧美激情第一欧美在线 | 成网站在线观看人免费 | 亚洲男人第一天堂 | 国产亚洲精品成人a在线 | 国产精品私人玩物在线观看 | 国产高清成人mv在线观看 | 欧美a级完整在线观看 | 日本亚洲成高清一区二区三区 | 97精品国产手机 | 99国产精品久久久久久久... | 午夜国产亚洲精品一区 | 精品欧美一区二区三区免费观看 | 免费看美女无遮掩的软件 | 成人黄激情免费视频 | 最近中文字幕在线 | 中文 |