当前位置: 移动技术网 > IT编程>开发语言>.net > .net core 图片合并,图片水印,等比例缩小,SixLabors.ImageSharp

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

2018年08月09日  | 移动技术网IT编程  | 我要评论

应运而生造句,台立法院旁突发枪击案,嬉戏谷水上乐园

需要引用 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/    如果对您有用 点个赞呦

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网