.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来操作图片,并给图片添加水印。
//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; }
}
相关文章
- 学习ASP.NET Core Blazor编程系列二——第一个Blazor应用程序(完)
- 学习ASP.NET Core Razor 编程系列五——Asp.Net Core Razor新建模板页面
- 学习ASP.NET Core Razor 编程系列四——Asp.Net Core Razor列表模板页面
- How do I force my .NET application to run as administrator?
- 下载远程(第三方服务器)文件、图片,保存到本地(服务器)的方法、保存抓取远程文件、图片 将图片的二进制字节字符串在HTML页面以图片形式输出 asp.net 文件 操作方法
- ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段
- WebAPI调用笔记 ASP.NET CORE 学习之自定义异常处理 MySQL数据库查询优化建议 .NET操作XML文件之泛型集合的序列化与反序列化 Asp.Net Core 轻松学-多线程之Task快速上手 Asp.Net Core 轻松学-多线程之Task(补充)
- C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序
- SQL 横转竖 、竖专横 (转载) 使用Dapper.Contrib 开发.net core程序,兼容多种数据库 C# 读取PDF多级书签 Json.net日期格式化设置 ASPNET 下载共享文件 ASPNET 文件批量下载 递归,循环,尾递归 利用IDisposable接口构建包含非托管资源对象 《.NET 进阶指南》读书笔记2------定义不可改变类型
- 项目开发中的一些注意事项以及技巧总结 基于Repository模式设计项目架构—你可以参考的项目架构设计 Asp.Net Core中使用RSA加密 EF Core中的多对多映射如何实现? asp.net core下的如何给网站做安全设置 获取服务端https证书 Js异常捕获
- 第七节:语法总结(1)(自动属性、out参数、对象初始化器、var和dynamic等) 图片放大镜 JavaScript-基础 用javascript写原生ajax(笔记) 初遇 Asp.net MVC 数据库依赖缓存那些事儿 前端JS 与 后台C# 之间JSON序列化与反序列化(笔记)
- bootstrap-data-target触发模态弹出窗元素的data使用 data-toggle与data-target的作用 深入ASP.NET MVC之九:Ajax支持 Asp.Net MVC4系列--进阶篇之AJAX
- ASP.NET MVC Filters 4种默认过滤器的使用【附示例】 数据库常见死锁原因及处理 .NET源码中的链表 多线程下C#如何保证线程安全? .net实现支付宝在线支付 彻头彻尾理解单例模式与多线程 App.Config详解及读写操作 判断客户端是iOS还是Android,判断是不是在微信浏览器打开
- C#设计模式总结 C#设计模式(22)——访问者模式(Vistor Pattern) C#设计模式总结 .NET Core launch.json 简介 利用Bootstrap Paginator插件和knockout.js完成分页功能 图片在线裁剪和图片上传总结 循序渐进学.Net Core Web Api开发系列【2】:利用Swagger调试WebApi
- (六)Net Core项目使用Controller之一 c# log4net 不输出日志 .NET Standard库引用导致的FileNotFoundException探究 获取json串里的某个属性值 common.js 如何调用common.js js 筛选数据 Join 具体用法
- C#字符串数组排序 C#排序算法大全 C#字符串比较方法 一个.NET通用JSON解析/构建类的实现(c#) C#处理Json文件 asp.net使用Jquery+iframe传值问题
- .net网站上传图片换电脑不显示 当不用网站的IP地址访问图片,只用相对路径访问时,在发布网站的时候,将上传图片的目标文件夹,包含在项目中再发布即可。
- ASP.NET MVC之Html.RenderAction(无操作方法 传参数)
- c# asp.net 编辑文件的摘要信息
- ImageResizer for .net 图片处理强大类库
- asp.net 截屏
- Json.net 时间格式处理
- ASP.NET Aries 入门开发教程5:自定义列表页工具栏区
- C# asp.net开源插件推荐:PdfiumViewer ( pdf 转成 图片png pdf convert to image )
- asp.net-Creating JavaScript objects from ASP.NET objects
- 接口 asp.net core 请求接口数据