.net core 圖片合並,圖片水印,等比例縮小,SixLabors.ImageSharp


需要引用 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    如果對您有用 點個贊呦

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM