C# 模仿微信生成九宫图头像
来源:互联网 发布:京东天猫淘宝哪个正品 编辑:程序博客网 时间:2024/06/02 04:11
/// <summary>/// 算出图片坐标/// </summary>/// <param name="size">合成数量</param>/// <returns></returns>public static string[] GetXy(int size){ string[] s = new string[size]; int _x = 0; int _y = 0; switch (size) { case 1: _x = _y = 6; s[0] = "6,6"; break; case 2: _x = _y = 4; s[0] = "4," + (132 / 2 - 60 / 2); s[1] = 60 + 2 * _x + "," + (132 / 2 - 60 / 2); break; case 3: _x = _y = 4; s[0] = (132 / 2 - 60 / 2) + "," + _y; s[1] = _x + "," + (60 + 2 * _y); s[2] = (60 + 2 * _y) + "," + (60 + 2 * _y); break; case 4: _x = _y = 4; s[0] = _x + "," + _y; s[1] = (_x * 2 + 60) + "," + _y; s[2] = _x + "," + (60 + 2 * _y); s[3] = (60 + 2 * _y) + "," + (60 + 2 * _y); break; case 5: _x = _y = 3; s[0] = (132 - 40 * 2 - _x) / 2 + "," + (132 - 40 * 2 - _y) / 2; s[1] = ((132 - 40 * 2 - _x) / 2 + 40 + _x) + "," + (132 - 40 * 2 - _y) / 2; s[2] = _x + "," + ((132 - 40 * 2 - _x) / 2 + 40 + _y); s[3] = (_x * 2 + 40) + "," + ((132 - 40 * 2 - _x) / 2 + 40 + _y); s[4] = (_x * 3 + 40 * 2) + "," + ((132 - 40 * 2 - _x) / 2 + 40 + _y); break; case 6: _x = _y = 3; s[0] = _x + "," + ((132 - 40 * 2 - _x) / 2); s[1] = (_x * 2 + 40) + "," + ((132 - 40 * 2 - _x) / 2); s[2] = (_x * 3 + 40 * 2) + "," + ((132 - 40 * 2 - _x) / 2); s[3] = _x + "," + ((132 - 40 * 2 - _x) / 2 + 40 + _y); s[4] = (_x * 2 + 40) + "," + ((132 - 40 * 2 - _x) / 2 + 40 + _y); s[5] = (_x * 3 + 40 * 2) + "," + ((132 - 40 * 2 - _x) / 2 + 40 + _y); break; case 7: _x = _y = 3; s[0] = (132 - 40) / 2 + "," + _y; s[1] = _x + "," + (_y * 2 + 40); s[2] = (_x * 2 + 40) + "," + (_y * 2 + 40); s[3] = (_x * 3 + 40 * 2) + "," + (_y * 2 + 40); s[4] = _x + "," + (_y * 3 + 40 * 2); s[5] = (_x * 2 + 40) + "," + (_y * 3 + 40 * 2); s[6] = (_x * 3 + 40 * 2) + "," + (_y * 3 + 40 * 2); break; case 8: _x = _y = 3; s[0] = (132 - 80 - _x) / 2 + "," + _y; s[1] = ((132 - 80 - _x) / 2 + _x + 40) + "," + _y; s[2] = _x + "," + (_y * 2 + 40); s[3] = (_x * 2 + 40) + "," + (_y * 2 + 40); s[4] = (_x * 3 + 40 * 2) + "," + (_y * 2 + 40); s[5] = _x + "," + (_y * 3 + 40 * 2); s[6] = (_x * 2 + 40) + "," + (_y * 3 + 40 * 2); s[7] = (_x * 3 + 40 * 2) + "," + (_y * 3 + 40 * 2); break; case 9: _y = 3; s[0] = _x + "," + _y; s[1] = _x * 2 + 40 + "," + _y; s[2] = _x * 3 + 40 * 2 + "," + _y; s[3] = _x + "," + (_y * 2 + 40); s[4] = (_x * 2 + 40) + "," + (_y * 2 + 40); s[5] = (_x * 3 + 40 * 2) + "," + (_y * 2 + 40); s[6] = _x + "," + (_y * 3 + 40 * 2); s[7] = (_x * 2 + 40) + "," + (_y * 3 + 40 * 2); s[8] = (_x * 3 + 40 * 2) + "," + (_y * 3 + 40 * 2); break; } return s;}
/// <summary>/// 图片宽度/// </summary>/// <param name="size">合成数量</param>/// <returns></returns>public static float GetWidth(int size){ int width = 0; if (size == 1) { width = 120; } if (size > 1 && size <= 4) { width = 60; } if (size >= 5) { width = 40; } return width;}
/// <summary>/// 把要合成的单张图片进行压缩/// </summary>/// <param name="bitmap">Bitmap</param>/// <param name="width">宽</param>/// <param name="height">高</param>/// <returns></returns>private Bitmap UserPir(Bitmap bitmap, int width, int height){ using (var bmp = new System.Drawing.Bitmap(width, height)) { //从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。 using (var gr = System.Drawing.Graphics.FromImage(bmp)) { //设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality gr.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality; //下面这个也设成高质量 gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //下面这个设成High gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //把原始图像绘制成上面所设置宽高的截取图 var rectDestination = new System.Drawing.Rectangle(0, 0, width, height); gr.DrawImage(bitmap, 0, 0, rectDestination, System.Drawing.GraphicsUnit.Pixel); //保存截取的图片 return bitmap; } }}
/// <summary>/// 合成头像/// </summary>/// <param name="imgPathList">图片路径集合</param>/// <param name="oldPath">原路径生成</param>/// <returns></returns>public string GroupPir(List<string> imgPathList, string oldPath = ""){ //绘制坐标 string[] arr = JiuGongDiagram.GetXy(imgPathList.Count); //图片尺寸 var wh = JiuGongDiagram.GetWidth(imgPathList.Count); using (Bitmap backgroudImg = new Bitmap(132, 132)) { using (Graphics g = Graphics.FromImage(backgroudImg)) { //设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //下面这个也设成高质量 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //下面这个设成High g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //清除画布,背景设置为灰色 g.Clear(System.Drawing.Color.Gainsboro); var flag = 0; MemoryStream ms; imgPathList.ForEach(m => { Bitmap bitmap; if (string.IsNullOrEmpty(m)) { bitmap = new Bitmap(HttpContext.Current.Server.MapPath(WebConfig.GroupUserPir)); } else { byte[] bytes = DownloadUserPir($"{WebConfig.FtpViewPath}{m}"); if (bytes.Length > 0) { ms = new MemoryStream(bytes); bitmap = new Bitmap(ms); ms.Close(); } else { bitmap = new Bitmap(HttpContext.Current.Server.MapPath(WebConfig.GroupUserPir)); } } string[] xy = arr[flag].Split(','); g.DrawImage(UserPir(bitmap, int.Parse(xy[0]), int.Parse(xy[1])), int.Parse(xy[0]), int.Parse(xy[1]), wh, wh); flag++; }); g.Dispose(); ms = new MemoryStream(); backgroudImg.Save(ms, System.Drawing.Imaging.ImageFormat.Png); if (string.IsNullOrEmpty(oldPath)) { //文件保存目录路径 string dirPath = "/Group/GroupPir/" + DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo); //文件名称 string fileName = $"{Utils.GetGuid(false).ToLower()}.png"; //保存的文件路径 var savePath = dirPath + "/" + fileName; return FTPUploadFiles.UploadMemoryStream(ms, dirPath, savePath); } return FTPUploadFiles.UploadMemoryStream(ms, oldPath) ? oldPath : string.Empty; } }}
/// <summary>/// 下载图片/// </summary>/// <param name="path">路径</param>/// <returns></returns>private byte[] DownloadUserPir(string path){ byte[] bt; try { using (WebClient webClient = new WebClient()) { bt = webClient.DownloadData(path); } } catch (Exception e) { bt = new byte[0]; } return bt;}
阅读全文
2 0
- C# 模仿微信生成九宫图头像
- 模仿微信九宫格图片展示控件
- Java 生成微信群头像 九宫格头像
- 模仿微信更改头像,图片局部放大
- 自定义PopWindow模仿微信拍照上传头像
- Python之itchat生成微信头像拼接问题
- 微信好友头像拼接图
- 微信上传头像
- 模仿微信红包
- 模仿微信界面
- C# 微信生成二维码
- 自定义ViewGroup打造微信朋友圈之九宫图效果
- 自定义ViewGroup打造微信朋友圈之九宫图效果
- 自定义ViewGroup打造微信朋友圈之九宫图效果
- 微信生成公众号带参数二维码用户头像 加上文字生成图片(三)
- ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果
- Android_ViewPage_Fragment_模仿微信界面
- 微信生成公众号带参数二维码加上用户头像(二)
- EXCEL中FIND函数与FindB函数
- java实现人脸识别源码【含测试效果图】——Service层(IUserService)
- windows中安装使用redis
- html5笔记
- asp.net core JWT 验证用户是否有效
- C# 模仿微信生成九宫图头像
- display:table-cell应用
- java实现人脸识别源码【含测试效果图】——ServiceImpl层(UserServiceImpl)
- HR面试
- oracle——数据定义
- vim E303问题解决办法
- 5.selenium 鼠标键盘组合使用
- android获取缩略图
- C++11 并发指南六( <atomic> 类型详解二 std::atomic )