.NET6使用ImageSharp实现给图片添加水印

 更新时间:2022年12月01日 08:16:12   作者:gmval  
这篇文章主要为大家详细介绍了.NET6使用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来操作图片,并给图片添加水印

//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有所区别,引用使用全路径
            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));
                }
                //水印图片完成成立,开始根据位置添加水印
                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;//设置不透明度,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>
        /// 上传状态
        /// </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; }
    }

到此这篇关于.NET6使用ImageSharp实现给图片添加水印的文章就介绍到这了,更多相关.NET ImageSharp图片添加水印内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • .net 通过URL推送POST数据具体实现

    .net 通过URL推送POST数据具体实现

    这篇文章主要介绍了.net 通过URL推送POST数据具体实现,有需要的朋友可以参考一下
    2013-12-12
  • WPF中自定义GridLengthAnimation

    WPF中自定义GridLengthAnimation

    这篇文章主要为大家详细介绍了WPF中自定义GridLengthAnimation的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • Asp.net MVC中Razor常见的问题与解决方法总结

    Asp.net MVC中Razor常见的问题与解决方法总结

    这篇文章主要给大家介绍了关于Asp.net MVC中Razor常见的问题与解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
    2017-08-08
  • ASP.net与SQLite数据库通过js和ashx交互(连接和操作)

    ASP.net与SQLite数据库通过js和ashx交互(连接和操作)

    这篇文章主要介绍了ASP.net与SQLite数据库通过js和ashx交互(连接和操作),具有一定的参考价值,有兴趣的可以了解一下。
    2017-01-01
  • Entity Framework代码优先Code First入门

    Entity Framework代码优先Code First入门

    这篇文章介绍了Entity Framework的代码优先模式Code First,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • asp.net core 获取 MacAddress 地址方法示例

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

    这篇文章主要介绍了asp.net core获取MacAddress地址方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02
  • asp.net批量多选文件上传解决方案

    asp.net批量多选文件上传解决方案

    这篇文章主要介绍了asp.net批量多选文件上传解决方案,基于flex开发的一个多选上传功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2015-08-08
  • Asp.Net Core中创建多DbContext并迁移到数据库的步骤

    Asp.Net Core中创建多DbContext并迁移到数据库的步骤

    这篇文章主要介绍了Asp.Net Core中创建多DbContext并迁移到数据库的步骤,帮助大家更好的理解和学习使用Asp.Net Core,感兴趣的朋友可以了解下
    2021-03-03
  • 部署.NET6项目到IIS

    部署.NET6项目到IIS

    这篇文章介绍了部署.NET6项目到IIS的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-12-12
  • ASP.NET MVC自定义错误页面真的简单吗?

    ASP.NET MVC自定义错误页面真的简单吗?

    ASP.NET MVC自定义错误页面真的简单吗?这篇文章主要介绍了ASP.NET MVC自定义错误页面,感兴趣的小伙伴们可以参考一下
    2016-10-10

最新评论