需要引用 SixLabors.ImageSharp 和SixLabors.ImageSharp.Drawing 引用方法 NuGet包管理 添加程式包來源 https://www.myget.org/F/imagesharp 包括預覽發行版 目前使用的是 1.0.0-beta0005 版本 3個引用 ...
需要引用 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 如果對您有用 點個贊呦