C# SixLabors.ImageSharp.Drawing的多种用途

在这里插入图片描述

生成验证码

/// <summary>
/// 生成二维码
/// </summary>
/// <param name="webRootPath">wwwroot目录</param>
/// <param name="verifyCode">验证码</param>
/// <param name="width">图片宽度</param>
/// <param name="height">图片高度</param>
/// <returns></returns>
public static byte[] CreateByteByImgVerifyCode(string webRootPath, string verifyCode, int width = 120, int height = 50)
{using Image image = new Image<Rgba32>(width, height);//漆底色白色image.Mutate(x => x.DrawLine(Pens.DashDot(Color.White, width), new PointF[] { new PointF() { X = 0, Y = 0 }, new PointF() { X = width, Y = height } }));FontCollection collection = new();FontFamily family = collection.Add(Path.Combine(webRootPath, "fonts", "FZHTJW.TTF"));Font font = family.CreateFont(20, FontStyle.Bold);PointF startPointF = new PointF(5, 5);Random random = new Random(); //随机数产生器Color[] colors = new Color[] { Color.Red, Color.Blue, Color.Green, Color.Purple, Color.Peru, Color.LightSeaGreen, Color.Lime, Color.Magenta, Color.Maroon, Color.MediumBlue, Color.MidnightBlue, Color.Navy };//绘制大小for (int i = 0; i < verifyCode.Length; i++){image.Mutate(x => x.DrawText(verifyCode[i].ToString(), font, colors[random.Next(colors.Length)], startPointF));startPointF.X += (int)(width - 10) / verifyCode.Length;startPointF.Y = random.Next(5, 10);}var pen = Pens.DashDot(Color.Silver, 1);//绘制干扰线for (var k = 0; k < 30; k++){PointF[] points = new PointF[2];points[0] = new PointF(random.Next(width), random.Next(height));points[1] = new PointF(random.Next(width), random.Next(height));image.Mutate(x => x.DrawLine(pen, points));}using MemoryStream stream = new MemoryStream();image.Save(stream, PngFormat.Instance);//输出图片流  return stream.ToArray();
}

压缩图片


/// <summary>
/// 生成缩略图并保存
/// </summary>
/// <param name="sFile">原图路径</param>
/// <param name="dFile">生成的缩略图路径</param>
/// <param name="dHeight">缩略图高度</param>
/// <param name="dWidth">缩略图宽度</param>
/// <param name="flag">压缩质量1-100</param>
/// <returns></returns>
public static bool GetPicThumbnail(string sFile, string dFile, int dHeight, int dWidth, int flag)
{Image sourceImage = null;FileStream compressImageFile = null;try{sourceImage = Image.Load(sFile);if (sourceImage == null){return false;}var originWidth = sourceImage.Width;var originHeight = sourceImage.Height;if (dWidth <= 0 && dHeight <= 0){//如果都是0就是原图dWidth = originWidth;dHeight = originHeight;}int sW;int sH;//按比例缩放if (originWidth > dWidth || originHeight > dHeight){if ((originWidth * dHeight) > (originHeight * dWidth)){sW = dWidth;sH = (dWidth * originHeight) / originWidth;}else{sH = dHeight;sW = (originWidth * dHeight) / originHeight;}}else{sW = originWidth;sH = originHeight;}缩放并且换转为灰度图//image.Mutate(x => x//         .Resize(image.Width / 2, image.Height / 2)  // 缩放//         .Grayscale());  // 转灰度图sourceImage.Mutate(x => x.Resize(sW, sH));//获取编码器var iamgeFormat = sourceImage.Metadata.DecodedImageFormat;IImageEncoder encoder = null;switch (iamgeFormat.Name.ToLower()){case "png":case "gif":case "bmp":break;default:encoder = new JpegEncoder(){Quality = flag //Use variable to set between 5-30 based on your requirements};break;}compressImageFile = new FileStream(dFile, FileMode.CreateNew, FileAccess.Write);if(encoder!=null){sourceImage.Save(compressImageFile, encoder);}else{sourceImage.Save(compressImageFile, iamgeFormat);}return true;}catch{return false;}finally{compressImageFile?.Dispose();sourceImage?.Dispose();}
}/// <summary>
/// 生成缩略图并返回byte数组
/// </summary>
/// <param name="sFile">原图路径</param>
/// <param name="dHeight">缩略图高度</param>
/// <param name="dWidth">缩略图宽度</param>
/// <param name="flag">压缩质量1-100</param>
/// <returns></returns>
public static byte[]? GetPicThumbnail(string sFile, int dHeight, int dWidth, int flag)
{Image sourceImage = null;MemoryStream compressImageFile = null;try{sourceImage = Image.Load(sFile);if (sourceImage == null){return null;}var originWidth = sourceImage.Width;var originHeight = sourceImage.Height;if (dWidth <= 0 && dHeight <= 0){//如果都是0就是原图dWidth = originWidth;dHeight = originHeight;}int sW;int sH;//按比例缩放if (originWidth > dWidth || originHeight > dHeight){if ((originWidth * dHeight) > (originHeight * dWidth)){sW = dWidth;sH = (dWidth * originHeight) / originWidth;}else{sH = dHeight;sW = (originWidth * dHeight) / originHeight;}}else{sW = originWidth;sH = originHeight;}sourceImage.Mutate(x => x.Resize(sW, sH));//获取编码器//获取编码器var iamgeFormat = sourceImage.Metadata.DecodedImageFormat;IImageEncoder encoder = null;switch (iamgeFormat.Name.ToLower()){case "png":case "gif":case "bmp":break;default:encoder = new JpegEncoder(){Quality = flag //Use variable to set between 5-30 based on your requirements};break;}compressImageFile = new MemoryStream();if (encoder != null){sourceImage.Save(compressImageFile, encoder);}else{sourceImage.Save(compressImageFile, iamgeFormat);}return compressImageFile.ToArray();}catch{return null;}finally{compressImageFile?.Dispose();sourceImage?.Dispose();}
}

生成海报

https://blog.csdn.net/qq_36437991/article/details/133383006

生成二维码

还需要依赖于QRCode库
在这里插入图片描述
有一个和image-sharp深度继承的库,它是跨平台的,不过目前最新版的qrcode应该也是跨平台的
在这里插入图片描述

/// <summary>
/// 将二维码转化为base64格式
/// </summary>
/// <param name="content">二维码内容</param>
/// <param name="iconPath">logo图片地址</param>
/// <returns></returns>
public string ToBase64QRCode(string content, string iconPath)
{QRCodeGenerator qrGenerator = new QRCodeGenerator();QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.H);var qrCode = new BitmapByteQRCode(qrCodeData);var qrCodeImage = qrCode.GetGraphic(5);if (string.IsNullOrEmpty(iconPath)){var res = Convert.ToBase64String(qrCodeImage);return res;}var iconImg = Image.Load(iconPath);iconImg.Mutate(x => x.Resize(60, 60, KnownResamplers.Box));var qrcodeImg = Image.Load(qrCodeImage);var left = qrcodeImg.Width / 2 - iconImg.Width / 2;var top = qrcodeImg.Height / 2 - iconImg.Height / 2;qrcodeImg.Mutate(t => t.DrawImage(iconImg, new Point(left, top), 1f));var qrcodeStream = new MemoryStream();qrcodeImg.Save(qrcodeStream, new PngEncoder());return Convert.ToBase64String(qrcodeStream.ToArray());
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/236907.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql 中in 和exists 的区别

左边大表用in、左边小表用exists in 适用于左边大表&#xff0c;右边小表。 exists 适用于左边小表&#xff0c;右边大表。 不管是用in&#xff0c;还是exists关键字&#xff0c;其核心思想都是用小表驱动大表。 in 适用于左边大表&#xff0c;右边小表。 exists 适用于左边小…

git仓库迁移,同步分支代码,并且去除仓库的历史提交记录

将原仓库迁移到一个新的仓库&#xff0c;并且不保留原仓库的历史提交记录。 1、创建新git仓库&#xff1a;全新的空git仓库。 2、将新仓库克隆到本地&#xff1a; git clone <新仓库的url> 3、将旧仓库master分支的代码合并到新仓库的master分支&#xff1a; 1、git…

【06】GeoScene海图或者电子航道图数据自动化质检

1 S-58错误管理器验证产品 在你编辑数据时进行快速的质量检查可以使用S-58错误管理器&#xff0c;S-58错误管理器工具允许您使用IHO S-58验证标准来验证海事数据库中的产品。你可以验证整个产品&#xff0c;或验证产品的当前范围。 1.1验证产品 使用S-58错误管理器工具完成以…

轻松实现 Linux 搭建 KMS 服务器,想做什么就做什么(附所有资料)

轻松实现 Linux 搭建 KMS 服务器,想做什么就做什么(附所有资料)。 支持产品: 下载 vlmcsd 下载文件并解压,把 binaries\Linux\intel\static\ 下的 vlmcsd-x64-musl-static 上传至 VPS/usr/bin/ 目录下,并改名为 vlmcsd。 给予执行权限 chmod +x /usr/bin/vlmcsd开启KM…

springboot整合shiro实现前后端分离,兼容最新的jakarta的依赖包(片尾推荐当前最好用的权限框架)

1.简单的用法如下ini realm方式 //1.创建数据源RealmDefaultSecurityManager defaultSecurityManager new DefaultSecurityManager();Ini ini Ini.fromResourcePath("classpath:shiro.ini");defaultSecurityManager.setRealm(new MyRealm()); defaultSecurityMana…

微信开发工具修改编译一直报Cannot read property ‘call‘ of undefined?

我个人的解决方法 更新HbuilderX和微信小程序开发者工具到最新版&#xff0c;微信开发者工具-设置-本地设置-调试基础库也换成最新的3.2.4&#xff0c;打开又报错&#xff0c; 把manifest.json文件内的 “mp-weixin” : {“libVersion”: “latest”}配置上就好了 如果不能解…

Axure基础

软件&#xff1a; 简单交互动效 动态面板 显示和隐藏 表单元件 表格设计 内联框架 导航菜单 元件交互样式 滚动屏幕与弹幕

java 4.数组

文章目录 4.数组4.1数组的概念4.2 数组的定义4.3 数组的初始化4.4 数组下标的有效范围与常见异常4.5 数组内存分析4.6 二维数组4.6.1 创建二维数组4.6.2 二维数组的赋值4.6.3 多维数组4.6.4 通过二维数组输出不同版式的古诗 4.7 不规则数组4.8 数组的基本操作4.8.1 数组遍历4.8…

数据结构和算法-平衡二叉树(定义 插入 删除 时间复杂度)

文章目录 平衡二叉树总览平衡二叉树的定义平衡二叉树的插入调整最小不平衡子树在A的左孩子的左子树中插入导致不平衡在A的右孩子的右子树中插入导致不平衡上述两种的代码思路在A的左孩子的右子树中插入导致不平衡在A的右孩子的左子树中插入导致不平衡 填个坑练习查找效率分析小…

锁相放大器(LIA)基本原理

本文介绍锁相放大器(LIA)基本原理。 锁相放大器(LIA)&#xff0c;英文名称&#xff1a;Lock-In Amplifier&#xff0c;在微弱信号检测领域使用非常广泛&#xff0c;比如科研电生理信号测量&#xff0c;传感器信号测量等。本文从理论上分析锁相放大器(LIA)基本原理。 1.基本概…

vivado生成时钟分析

生成的时钟 本节讨论生成的时钟&#xff0c;包括&#xff1a; •关于生成的时钟 •用户定义的生成时钟 •自动衍生时钟 •自动衍生时钟 关于生成的时钟 生成的时钟在设计内部由称为时钟修改块&#xff08;用于例如MMCM&#xff09;&#xff0c;或者通过一些用户逻辑。生…

如何在Linux中查看正在运行的进程以及过滤特定端口和进程名称

在Linux系统中&#xff0c;管理和监控正在运行的进程是非常重要的。以下是一些常用的命令和技巧&#xff0c;帮助你查看、筛选和管理Linux中的进程。 1. 查看所有正在运行的进程 使用ps命令 ps命令是查看进程状态的基本工具。以下是一些常见的用法&#xff1a; 显示当前终端…

NFS搭建

离线环境&#xff0c;提前在有网络的服务器上下载好需要的软件包 yum -y install nfs-utils rpcbind --downloadonly --downloaddir /root/nfs zip -r nfs.zip nfs/ registry.cn-beijing.aliyuncs.com/pylixm/nfs-subdir-external-provisioner:v4.0.0 #nfs 安装 unzip nfs.zi…

mysql 表锁 行锁

目录 表锁&#xff08;Table Lock&#xff09; 行锁&#xff08;Row Lock&#xff09; 进一步通过举例解释 update操作走的是什么锁 表锁示例&#xff1a; 行锁示例&#xff1a; MySQL 中常见的锁类型包括&#xff1a; 表锁&#xff08;Table Lock&#xff09; 是针对整个…

redisson 哨兵模式配置

背景&#xff1a;项目redis由集群改为哨兵模式&#xff0c;漏洞扫描未授权访问漏洞&#xff08;CNVD-2019-21763&#xff09;&#xff0c;要求对redis哨兵也设置密码&#xff0c;redisson依赖版本为3.11.5 spring-boot版本为2.1.13。 redisson依赖升级 <dependency>&l…

[JS设计模式]Command Pattern

文章目录 举例说明优点缺点完整代码 With the Command Pattern, we can decouple objects that execute a certain task from the object that calls the method. 使用命令模式&#xff0c;我们可以将执行特定任务的对象与调用该方法的对象解耦。 怎么理解 执行特定任务的对…

基于Java (spring-boot)的课程管理系统

一、项目介绍 ​近年来&#xff0c;随着网络学校规模的逐渐增大&#xff0c;人工书写数据已经不能够处理如此庞大的数据。为了更好的适应信息时代的高效性&#xff0c;一个利用计算机来实现学生信息管理工作的系统将必然诞生。基于这一点&#xff0c;设计了一个学生信息管理系统…

Mybatis基本操作

目录 准备工作 删除操作 预编译SQL 增加操作 获取返回的主键 更新操作 准备工作 准备数据库表 emp创建一个新的springboot工程&#xff0c;选择引入对应的起步依赖&#xff08;mybatis、mysql驱动、lombok&#xff09;application.properties中引入数据库连接信息创建对应…

html 内外边距区别以及解释

引入&#xff1a; 我们在之前的学习中学习了边框&#xff0c;我们发现只要是页面的标签元素&#xff0c;都可以实现边框的效果&#xff0c;那么接下来我们来讲解一个比较重要的知识点&#xff0c;边距&#xff0c;边距分为内边距和外边距&#xff0c;它们和边框一起是我们后面学…

Mybatis配置-映射器(mappers)

现在&#xff0c;我们已经配置了MyBatis的行为&#xff0c;准备定义我们的映射SQL语句。但首先&#xff0c;我们需要告诉MyBatis在哪里找到它们。在这方面&#xff0c;Java并没有提供很好的自动发现机制&#xff0c;所以最好的方法是直接告诉MyBatis在哪里找到映射文件。 您可以…