.net core微信群图片合并

引用:SixLabors.ImageSharp,SixLabors.ImageSharp.Drawing,System.Drawing.Common

   /// <summary> /// 群组图片合并 /// </summary> public static class ImageSharpHelper { /// <summary> /// 背景图片缩略图大小 /// </summary> public static int BackHeigth = 210; #region 获取合并后的图片 /// <summary> /// 获取合并后的图片 /// </summary> /// <param name="ImageFile">要合并的图片路径</param> /// <returns></returns> public static ApiResult GetImageSharp(List<string> ImageFile) { if (ImageFile.Count <= 1 || ImageFile.Count > 9) { return ApiResult.Fail(ApiEnum.OtherError, "合并的图片只能2到9张"); } //背景图 var aBase64Str = @"iVBORw0KGgoAAAANSUhEUgAAALEAAACxCAIAAAAES8uSAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyZpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMTM4IDc5LjE1OTgyNCwgMjAxNi8wOS8xNC0wMTowOTowMSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENDIDIwMTcgKFdpbmRvd3MpIiB4bXBNTTpJbnN0YW5jZUlEPSJ4bXAuaWlkOkE4OTBFNDM1OTMwQjExRTlBN0ZBOTQyQzFCQjg0MzRBIiB4bXBNTTpEb2N1bWVudElEPSJ4bXAuZGlkOkE4OTBFNDM2OTMwQjExRTlBN0ZBOTQyQzFCQjg0MzRBIj4gPHhtcE1NOkRlcml2ZWRGcm9tIHN0UmVmOmluc3RhbmNlSUQ9InhtcC5paWQ6QTg5MEU0MzM5MzBCMTFFOUE3RkE5NDJDMUJCODQzNEEiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6QTg5MEU0MzQ5MzBCMTFFOUE3RkE5NDJDMUJCODQzNEEiLz4gPC9yZGY6RGVzY3JpcHRpb24+IDwvcmRmOlJERj4gPC94OnhtcG1ldGE+IDw/eHBhY2tldCBlbmQ9InIiPz7toEL7AAABQ0lEQVR42uzSQREAAAQAQfRPoiRfHexGuLmcDrhKAjyBJ/AEnsATeAJP4Ak8gSfwBJ7AE3gCT+AJ8ASewBN4Ak/gCTyBJ/AEnsATeAJP4Ak8gSfAE3gCT+AJPIEn8ASewBN4Ak/gCTyBJ/AEngBP4Ak8gSfwBJ7AE3gCT+AJPIEn8ASewBN4AjyBJ/AEnsATeAJP4Ak8gSfwBJ7AE3gCT+AJ8ASewBN4Ak/gCTyBJ/AEnsATeAJP4Ak8gSfAE3gCT+AJPIEn8ASewBN4Ak/gCTyBJ/AEngBP4Ak8gSfwBJ7AE3gCT+AJPIEn8ASewBN4Ak9IgCfwBJ7AE3gCT+AJPIEn8ASewBN4Ak/gCTwBnsATeAJP4Ak8gSfwBJ7AE3gCT+AJPIEn8AR4Ak/gCTyBJ/AEnsATeAJP4Ak8gSfwBI+sAAMAmMcDC6uBzccAAAAASUVORK5CYII="; List<byte[]> list = new List<byte[]>(); foreach (var item in ImageFile) { list.Add(GetImageByte(item)); } return ImageCaoByte(Convert.FromBase64String(aBase64Str), list); } #endregion #region 合并图片 base64 List集合 /// <summary> /// 合并图片 base64 List集合 /// </summary> /// <param name="backBase64">背景图片</param> /// <param name="ListBase64">合成图片集合</param> /// <returns></returns> public static ApiResult ImageCaoBase64(string backBase64, List<string> ListBase64) { List<byte[]> list = new List<byte[]>(); foreach (var item in ListBase64) { list.Add(Convert.FromBase64String(item)); } return ImageCaoByte(Convert.FromBase64String(backBase64), list); #region 单张测试 //byte[] templebytes = Convert.FromBase64String(aBase64Str); //byte[] outputbytes = Convert.FromBase64String(bBase64Str); //System.Drawing.Image temple = MakeThumbnail(templebytes, 200, 200, ThumbnailMode.Cut); //System.Drawing.Image output = MakeThumbnail(outputbytes, 100, 100, ThumbnailMode.Cut); //var templeNailByte = ImgToByt(temple); //var outputNailByte = ImgToByt(output); //IImageFormat format = null; //Image<Rgba32> templeRgba32= SixLabors.ImageSharp.Image.Load(templeNailByte, out format); //Image<Rgba32> outputRgba32 = SixLabors.ImageSharp.Image.Load(outputNailByte); //var result = MergeImageRgba32(templeRgba32, outputRgba32, 0, 0, format); //return result; #endregion } #endregion #region 合并图片 字节 List集合 /// <summary> /// 合并图片 字节 List集合 /// </summary> /// <param name="backBase64">背景图片</param> /// <param name="ListBase64">合成图片集合</param> /// <returns></returns> public static ApiResult ImageCaoByte(byte[] backBase64, List<byte[]> ListBase64) { //base64转字节 byte[] templebytes = backBase64;// Convert.FromBase64String(backBase64); //缩略图 System.Drawing.Image temple = MakeThumbnail(templebytes, BackHeigth, BackHeigth, ThumbnailMode.Cut); //图片转字节 byte[] templeNailByte = ImgToByt(temple); ApiResult result = ApiResult.Fail(ApiEnum.OtherError, "请求失败"); var ListCount = ListBase64.Count; for (int i = 0; i < ListCount; i++) { byte[] outputbytes = ListBase64[i]; System.Drawing.Image output = MakeThumbnail(outputbytes, SizeWidth(ListCount, BackHeigth), SizeHeight(ListCount, BackHeigth), ThumbnailMode.Cut); byte[] outputNailByte = ImgToByt(output); Image<Rgba32> outputRgba32 = SixLabors.ImageSharp.Image.Load(outputNailByte); //背景图 IImageFormat format = null; Image<Rgba32> templeRgba32 = SixLabors.ImageSharp.Image.Load(templeNailByte, out format); //合并图片 result = MergeImageRgba32(templeRgba32, outputRgba32, XWidth(ListCount, i + 1, BackHeigth), YHeight(ListCount, i + 1, BackHeigth), format); if (i < ListCount - 1 && result.Status) { var str = result.Data.ToString(); var dataBase64 = str.Substring(str.IndexOf(',') + 1); templeNailByte = Convert.FromBase64String(dataBase64); } } return result; } #endregion #region 合并图片 小图片放在大图片上面 MergeImageBase64 /// <summary> /// 合并图片 小图片放在大图片上面 MergeImageBase64 /// </summary> /// <param name="TempleBase64Str">模板大图片base64</param> /// <param name="OutputBase64Str">模板小图片base64</param> /// <param name="x">X坐标</param> /// <param name="y">y坐标</param> /// <returns></returns> public static ApiResult MergeImageBase64(string TempleBase64Str, string OutputBase64Str, int x, int y) { string strRet = null; if (string.IsNullOrEmpty(TempleBase64Str)) { return ApiResult.Fail(ApiEnum.ParameterError, "请传入模板大图片base64"); } if (string.IsNullOrEmpty(OutputBase64Str)) { return ApiResult.Fail(ApiEnum.ParameterError, "请传入模板小图片base64"); } if (x < 0 || y < 0) { return ApiResult.Fail(ApiEnum.ParameterError, "坐标不能传入负数"); } 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 ApiResult.Fail(ApiEnum.ParameterError, "Y坐标高度超限"); } if (imagesTemle.Width - (outputImg.Width + x) <= 0) { return ApiResult.Fail(ApiEnum.ParameterError, "X坐标宽度超限"); } //进行多图片处理 imagesTemle.Mutate(a => { //还是合并 a.DrawImage(outputImg, new SixLabors.Primitives.Point(x, y), 1); }); strRet = imagesTemle.ToBase64String(format); return ApiResult.Success(strRet); } catch (Exception ex) { return ApiResult.Fail(ApiEnum.ParameterError, "报错信息" + ex.Message); } } #endregion #region 合并图片 小图片放在大图片上面 MergeImageRgba32 /// <summary> /// 合并图片 小图片放在大图片上面 MergeImageRgba32 /// </summary> /// <param name="TempleByte">模板大图片Byte</param> /// <param name="OutputBaseByte">模板小图片Byte</param> /// <param name="x">X坐标</param> /// <param name="y">y坐标</param> /// <param name="format">format</param> /// <returns></returns> public static ApiResult MergeImageRgba32(Image<Rgba32> TempleByte, Image<Rgba32> OutputBaseByte, int x, int y, IImageFormat format) { string strRet = null; if (x < 0 || y < 0) { return ApiResult.Fail(ApiEnum.ParameterError, "坐标不能传入负数"); } try { var imagesTemle = TempleByte; var outputImg = OutputBaseByte; if (imagesTemle.Height - (outputImg.Height + y) < 0) { return ApiResult.Fail(ApiEnum.ParameterError, "Y坐标高度超限"); } if (imagesTemle.Width - (outputImg.Width + x) < 0) { return ApiResult.Fail(ApiEnum.ParameterError, "X坐标宽度超限"); } //进行多图片处理 imagesTemle.Mutate(a => { //还是合并 a.DrawImage(outputImg, new SixLabors.Primitives.Point(x, y), 1); }); strRet = imagesTemle.ToBase64String(format); return ApiResult.Success(strRet, "成功"); } catch (Exception ex) { return ApiResult.Fail(ApiEnum.ParameterError, "报错信息" + ex.Message); } } #endregion #region 生成缩略图 /// <summary> /// 生成缩略图 /// </summary> /// <param name="imgBytes">源文件字节数组</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">缩略图方式</param> public static System.Drawing.Image MakeThumbnail(byte[] imgBytes, int width, int height, ThumbnailMode mode) { using (var sourceImage = FromBytes(imgBytes)) { return MakeThumbnailThis(sourceImage, width, height, mode); } } /// <summary> /// 生成缩略图 /// </summary> /// <param name="sourceImage">源图</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">缩略图方式</param> public static System.Drawing.Image MakeThumbnailThis(System.Drawing.Image sourceImage, int width, int height, ThumbnailMode mode) { var towidth = width; var toheight = height; var x = 0; var y = 0; var ow = sourceImage.Width; var oh = sourceImage.Height; switch (mode) { case ThumbnailMode.FixedBoth: break; case ThumbnailMode.FixedW: toheight = oh * width / ow; break; case ThumbnailMode.FixedH: towidth = ow * height / oh; break; case ThumbnailMode.Cut: if (ow / (double)oh > towidth / (double)toheight) { oh = sourceImage.Height; ow = sourceImage.Height * towidth / toheight; y = 0; x = (sourceImage.Width - ow) / 2; } else { ow = sourceImage.Width; oh = sourceImage.Width * height / towidth; x = 0; y = (sourceImage.Height - oh) / 2; } break; } //1、新建一个BMP图片 var bitmap = new Bitmap(towidth, toheight); //2、新建一个画板 var g = Graphics.FromImage(bitmap); try { //3、设置高质量插值法 g.InterpolationMode = InterpolationMode.High; //4、设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //5、清空画布并以透明背景色填充 g.Clear(Color.Transparent); //6、在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(sourceImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); return bitmap; } catch (Exception e) { throw e; } finally { g.Dispose(); } } #endregion #region 字节转换成图片流 /// <summary> /// 字节转换成图片流 /// </summary> /// <param name="bytes">字节数组</param> /// <returns></returns> public static System.Drawing.Image FromBytes(byte[] bytes) { using (var ms = new MemoryStream(bytes)) { return System.Drawing.Image.FromStream(ms); } } #endregion #region 图片流转换成字节 /// <summary> /// 图片流转换成字节 /// </summary> /// <param name="img">要转换的Image对象</param> /// <returns>转换后返回的字节流</returns> public static byte[] ImgToByt(System.Drawing.Image img) { MemoryStream ms = new MemoryStream(); byte[] imagedata = null; img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); imagedata = ms.GetBuffer(); return imagedata; } #endregion #region 根据http图片路径返回图片的字节流byte[] /// <summary> /// 根据http图片路径返回图片的字节流byte[] /// </summary> /// <param name="imagePath">图片路径</param> /// <returns>返回的字节流</returns> public static byte[] GetImageByte(string imagePath) { WebClient client = new WebClient(); byte[] pageData = client.DownloadData(imagePath); return pageData; } #endregion #region 根据本机图片路径返回图片的字节流byte[] /// <summary> /// 根据本机图片路径返回图片的字节流byte[] /// </summary> /// <param name="imagePath">图片路径</param> /// <returns>返回的字节流</returns> public static byte[] GetImageByteIam(string imagePath) { FileStream files = new FileStream(imagePath, FileMode.Open); byte[] imgByte = new byte[files.Length]; files.Read(imgByte, 0, imgByte.Length); files.Close(); return imgByte; } #endregion #region 获取小的缩略图高度 /// <summary> /// 获取小的缩略图高度 /// </summary> /// <param name="Sum">总张数</param> /// <param name="BackHeigth">背景图片的大小</param> /// <returns></returns> public static int SizeHeight(int Sum, int BackHeigth) { if (Sum <= 4) { return BackHeigth / 2; } else if (Sum > 4 && Sum <= 9) { return BackHeigth / 3; } return 0; } #endregion #region 获取小的缩略图宽度 /// <summary> /// 获取小的缩略图宽度 /// </summary> /// <param name="Sum">总张数</param> /// <param name="BackWidth">背景图片的大小</param> /// <returns></returns> public static int SizeWidth(int Sum, int BackWidth) { if (Sum <= 4) { return BackHeigth / 2; } else if (Sum > 4 && Sum <= 9) { return BackHeigth / 3; } return 0; } #endregion #region 获取X坐标点 /// <summary> /// 获取X坐标点 /// </summary> /// <param name="Sum">总张数</param> /// <param name="Several">第几张图片</param> /// <param name="BackHeigth">背景图片的大小</param> /// <returns></returns> public static int XWidth(int Sum, int Several, int BackHeigth) { if (Sum == 2) { return (Several - 1) * (BackHeigth / 2); } else if (Sum > 2 && Sum <= 4) { if (Several % 2 == 1) { return 0; } else { return BackHeigth / 2; } } else if (Sum > 4 && Sum <= 9) { if (Several % 3 == 1) { return 0; } else if (Several % 3 == 2) { return BackHeigth / 3; } else { return (BackHeigth / 3) * 2; } } return 0; } #endregion #region 获取Y坐标点 /// <summary> /// 获取Y坐标点 /// </summary> /// <param name="Sum">总张数</param> /// <param name="Several">第几张图片</param> /// <param name="BackHeigth">背景图片的大小</param> /// <returns></returns> public static int YHeight(int Sum, int Several, int BackHeigth) { if (Sum == 2) { return BackHeigth / 4; } else if (Sum > 2 && Sum <= 4) { if (Several <= 2) { return 0; } else { return BackHeigth / 2; } } else if (Sum > 4 && Sum <= 6) { if (Several <= 3) { return BackHeigth / 6; } else { return BackHeigth / 2; } } else if (Sum > 6 && Sum <= 9) { if (Several <= 3) { return 0; } else if (Several > 3 && Several <= 6) { return BackHeigth / 3; } else { return (BackHeigth / 3) * 2; } } return 0; } #endregion } /// <summary> /// 缩略图模式 /// </summary> public enum ThumbnailMode { /// <summary> /// 指定宽高裁剪(不变形) /// </summary> Cut = 1, /// <summary> /// 指定宽度,高度自动 /// </summary> FixedW = 2, /// <summary> /// 指定高度,宽度自动 /// </summary> FixedH = 4, /// <summary> /// 指定宽高(变形) /// </summary> FixedBoth = 8 }

 

posted @ 2019-08-07 14:41  Smile灬Lucky  阅读(1154)  评论(1编辑  收藏  举报