分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

.net core 图片合并,图片水印,等比例缩小,SixLabors.ImageSharp

发布时间:2023-09-06 02:09责任编辑:郭大石关键词:暂无标签

需要引用 SixLabors.ImageSharp 和SixLabors.ImageSharp.Drawing

引用方法 NuGet包管理 

添加程序包来源 https://www.myget.org/F/imagesharp  包括预览发行版  目前使用的是 1.0.0-beta0005 版本

 3个引用

using SixLabors.ImageSharp;using SixLabors.ImageSharp.Formats;using SixLabors.ImageSharp.Processing;

 1图片与二维码合并

/// <summary> ???????/// 合并图片 小图片放在大图片上面 ???????/// </summary> ???????/// <param name="TempleBase64Str">模板大图片base64</param> ???????/// <param name="OutputBase64Str">模板小图片base64</param> ???????/// <param name="x">X坐标</param> ???????/// <param name="y">y坐标</param> ???????/// <returns></returns> ???????public ImageResponse MergeImage(string TempleBase64Str, string OutputBase64Str, int x, int y) ???????{ ???????????string strRet = null; ???????????if (string.IsNullOrEmpty(TempleBase64Str)) ???????????{ ???????????????return new ImageResponse { success = false, errmsg = "请传入模板大图片base64" }; ???????????} ???????????if (string.IsNullOrEmpty(OutputBase64Str)) ???????????{ ???????????????return new ImageResponse { success = false, errmsg = "请传入模板小图片base64" }; ???????????} ???????????if (x < 0 || y < 0) ???????????{ ???????????????return new ImageResponse { success = false, errmsg = "坐标不能传入负数" }; ???????????} ???????????try ???????????{ ???????????????byte[] templebytes = Convert.FromBase64String(TempleBase64Str); ???????????????byte[] outputbytes = Convert.FromBase64String(OutputBase64Str); ???????????????IImageFormat format = null; ???????????????var imagesTemle = SixLabors.ImageSharp.Image.Load(templebytes, out format); ???????????????var outputImg = SixLabors.ImageSharp.Image.Load(outputbytes); ???????????????????????????????if (imagesTemle.Height - (outputImg.Height + y) <= 0) ???????????????{ ???????????????????return new ImageResponse { success = false, errmsg = "Y坐标高度超限" }; ???????????????} ???????????????if (imagesTemle.Width - (outputImg.Width + x) <= 0) ???????????????{ ???????????????????return new ImageResponse { success = false, errmsg = "X坐标宽度超限" }; ???????????????} ???????????????//进行多图片处理 ???????????????imagesTemle.Mutate(a => ???????????????{ ???????????????????//还是合并 ????????????????????a.DrawImage(outputImg, 1, new SixLabors.Primitives.Point(x, y)); ???????????????}); ???????????????strRet = imagesTemle.ToBase64String(format); ???????????????return new ImageResponse { success = true, base64Str = strRet }; ???????????} ???????????catch (Exception ex) ????????????{ ???????????????return new ImageResponse { success = false, errmsg ="报错信息"+ex.Message }; ???????????} ???????}

2缩小倍数 

outputImg.Mutate(ctx => ctx.Resize(outputImg.Width / 2, outputImg.Height / 2));

a.DrawImage(outputImg, 1, new SixLabors.Primitives.Point(x, y)); ??//参数1 范围是0-1 代表的模糊程度

最后生成的图片就是下面的样子 不过返回的是图片的base64字符串 

byte[] 也可以转换从成

using (Stream fs = new MemoryStream(bytes))//路径参数using(FileStream streamTemple=System.IO.File.OpenRead("c/图片路径"))using(MemoryStream output=new MemoryStream()){ ???... ???//保存图片 ???imagesTemle.SaveAsJpeg(streamTemple); ???streamTemple.Close(); ???...}

这里是 官方文档 所有的功能详情 在这里面  https://sixlabors.github.io/docs/api/index.html    如果对您有用 点个赞呦

.net core 图片合并,图片水印,等比例缩小,SixLabors.ImageSharp

原文地址:https://www.cnblogs.com/AnkerZhang/p/9447815.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved