IT评测·应用市场-qidao123.com

标题: .NET 6使用ImageSharp给图片添加水印 [打印本页]

作者: 小秦哥    时间: 2022-11-30 18:41
标题: .NET 6使用ImageSharp给图片添加水印

.NET 6 中,使用System.Drawing操作图片,生成解决方案或打包的时候,会有警告,意思是System.Drawing仅在 'windows' 上受支持。微软官方的解释是:
System.Drawing.Common NuGet 包现在被归为 Windows 特定的库。 在为非 Windows 操作系统编译时,平台分析器会在编译时发出警告。
在非 Windows 操作系统上,除非设置了运行时配置开关,否则将引发 TypeInitializationException 异常,其中 PlatformNotSupportedException 作为内部异常
在 .NET 6 之前,使用 System.Drawing.Common 包不会产生任何编译时警告,也不会引发任何运行时异常。
从 .NET 6 开始,当为非 Windows 操作系统编译引用代码时,平台分析器会发出编译时警告。
当然,使用windows操作系统没有任何问题,Linux的话,需要单独的配置。
可以通过在runtimeconfig.json文件中将System.Drawing.EnableUnixSupport 运行时配置开关设置为来启用对 .NET 6 中的非 Windows 平台的支持:true
或者使用第三方库
ImageSharp
SkiaSharp
Microsoft.Maui.Graphics
正如标题,我使用了ImageSharp来操作图片,并给图片添加水印
  1. //ImageFile为图片物理路径,如下方的注释
  2.         public async Task<ImageResult> WaterMark(string ImageFile)
  3.         {
  4.             ImageResult result = new ImageResult();
  5.             //var ImageFile = "D:\www\wwwroot\upload\5176caebc1404caa8b0b350181ae28ab.jpg";
  6.             var WaterMark = "D:\\www\\wwwroot\\watermark.png";
  7.             string FileName = Guid.NewGuid().ToString("N") + ".jpg";
  8.             string SavePath = "D:\\www\\wwwrootupload\" + FileName;
  9.             string imgurl = "/upload/"+FileName;
  10.             //为了与System.Drawing.Common有所区别,引用使用全路径
  11.             using (var image = await SixLabors.ImageSharp.Image.LoadAsync(ImageFile))
  12.             {
  13.                 using (var clone = image.Clone(ctx => ctx.ApplyScalingImageWaterMark("center")))
  14.                 {
  15.                     await clone.SaveAsync(SavePath);
  16.                 }
  17.                 result.width = image.Width;
  18.                 result.height = image.Height;
  19.                 result.url = imgurl;
  20.                 result.format = ".jpg";
  21.                 result.state = true;
  22.             }
  23.             return result;
  24.         }
复制代码
代码比较简单,首先使用SixLabors.ImageSharp.Image.LoadAsync打开图片,然后使用ImageSharp的自定义扩展方法给图片添加水印。
ApplyScalingImageWaterMark扩展方法:
  1. public static class ImageSharpExtention
  2. {
  3.     public static IImageProcessingContext ApplyScalingImageWaterMark(this IImageProcessingContext processingContext, string waterPosition = "center",string waterPath)
  4.     {
  5.          using (var mark_image = SixLabors.ImageSharp.Image.Load(waterPath))
  6.             {
  7.                 int markWidth = mark_image.Width;
  8.                 int markHeight = mark_image.Height;
  9.                 var imgSize = processingContext.GetCurrentSize();
  10.                 if (markWidth >= imgSize.Width || markHeight >= imgSize.Height) //对水印图片进行缩放
  11.                 {
  12.                     if (imgSize.Width > imgSize.Height)//横的长方形
  13.                     {
  14.                         markWidth = imgSize.Width / 2; //宽缩放一半
  15.                         markHeight = (markWidth * imgSize.Height) / imgSize.Width;
  16.                     }
  17.                     else
  18.                     {
  19.                         markHeight = imgSize.Height / 2;
  20.                         markWidth = (markHeight * imgSize.Width) / imgSize.Height;
  21.                     }
  22.                     mark_image.Mutate(mk => mk.Resize(markWidth, markHeight));
  23.                 }
  24.                 //水印图片完成成立,开始根据位置添加水印
  25.                 var position = waterPosition;
  26.                 if (string.IsNullOrEmpty(position))
  27.                 {
  28.                     position = "center";
  29.                 }
  30.                 position = position.ToLower();
  31.                 if (string.IsNullOrEmpty(position))
  32.                 {
  33.                     position = "center";
  34.                 }
  35.                 SixLabors.ImageSharp.Point point = new SixLabors.ImageSharp.Point();
  36.                 //左上
  37.                 if (position.Contains("lefttop"))
  38.                 {
  39.                     point.X = 10;
  40.                     point.Y = 10;
  41.                 }
  42.                 //上中
  43.                 if (position.Contains("topcenter"))
  44.                 {
  45.                     point.X = (imgSize.Width - mark_image.Width) / 2;
  46.                     point.Y = 10;
  47.                 }
  48.                 //右上
  49.                 if (position.Contains("righttop"))
  50.                 {
  51.                     point.X = (imgSize.Width - mark_image.Width) - 10;
  52.                     point.Y = 10;
  53.                 }
  54.                 //右中
  55.                 if (position.Contains("rightcenter"))
  56.                 {
  57.                     point.X = (imgSize.Width - mark_image.Width) - 10;
  58.                     point.Y = (imgSize.Height - mark_image.Height) / 2;
  59.                 }
  60.                 //右下
  61.                 if (position.Contains("rightbottom"))
  62.                 {
  63.                     point.X = (imgSize.Width - mark_image.Width) - 10;
  64.                     point.Y = (imgSize.Height - mark_image.Height) - 10;
  65.                 }
  66.                 //下中
  67.                 if (position.Contains("bottomcenter"))
  68.                 {
  69.                     point.X = (imgSize.Width - mark_image.Width) / 2;
  70.                     point.Y = (imgSize.Height - mark_image.Height) - 10;
  71.                 }
  72.                 //左下
  73.                 if (position.Contains("leftbottom"))
  74.                 {
  75.                     point.X = 10;
  76.                     point.Y = (imgSize.Height - mark_image.Height) - 10;
  77.                 }
  78.                 //左中
  79.                 if (position.Contains("leftcenter"))
  80.                 {
  81.                     point.X = 10;
  82.                     point.Y = (imgSize.Height - mark_image.Height) / 2;
  83.                 }
  84.                 if (position.Contains("center"))
  85.                 {
  86.                     point.X = (imgSize.Width - mark_image.Width) / 2;
  87.                     point.Y = (imgSize.Height - mark_image.Height) / 2;
  88.                 }
  89.                 float opacity=(float)0.8;//设置不透明度,0-1之间
  90.                
  91.                 //添加水印
  92.                 return processingContext.DrawImage(mark_image,point,opacity);
  93.             }
  94.     }
  95. }
复制代码
ImageResult类:
  1. public class ImageResult
  2.     {
  3.         /// <summary>
  4.         /// 文件名
  5.         /// </summary>
  6.         public string id { get; set; }
  7.         /// <summary>
  8.         /// 文件大小
  9.         /// </summary>
  10.         public string size { get; set; }
  11.         /// <summary>
  12.         /// 文件路径
  13.         /// </summary>
  14.         public string url { get; set; }
  15.         /// <summary>
  16.         /// 文件格式
  17.         /// </summary>
  18.         public string format { get; set; }
  19.         /// <summary>
  20.         /// 上传状态
  21.         /// </summary>
  22.         public bool state { get; set; }
  23.         /// <summary>
  24.                 /// 上传消息
  25.                 /// </summary>
  26.                 public string msg { get; set; }
  27.         /// <summary>
  28.         /// 图片宽
  29.         /// </summary>
  30.         public int width { get; set; }
  31.         /// <summary>
  32.         /// 图片高
  33.         /// </summary>
  34.         public int height { get; set; }
  35.     }
复制代码
如有不正确的地方,还望不吝指教。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4