您的位置:首页 > 移动开发 > 微信开发

C# 模仿微信生成九宫图头像

2017-09-26 11:55 483 查看
/// <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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: