平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

117 lines
3.5 KiB

  1. //
  2. namespace SafeCampus.Core.Utils;
  3. /// <summary>
  4. /// 头像功能
  5. /// </summary>
  6. public static class AvatarUtil
  7. {
  8. #region 姓名生成图片处理
  9. /// <summary>
  10. /// 获取姓名对应的颜色值
  11. /// </summary>
  12. /// <param name="name"></param>
  13. /// <returns></returns>
  14. public static string GetNameColor(string name)
  15. {
  16. if (string.IsNullOrEmpty(name) || name.Length <= 0)
  17. throw new Exception("name不能为空");
  18. //获取名字第一个字,转换成 16进制 图片
  19. var str = "";
  20. foreach (var item in name)
  21. {
  22. str += Convert.ToUInt16(item);
  23. }
  24. if (str.Length < 4)
  25. {
  26. str += new Random().Next(100, 1000);
  27. }
  28. var color = "#" + str.Substring(1, 3);
  29. return color;
  30. }
  31. /// <summary>
  32. /// 获取姓名对应的图片
  33. /// </summary>
  34. /// <param name="name"></param>
  35. /// <param name="width"></param>
  36. /// <param name="height"></param>
  37. /// <returns></returns>
  38. public static SKBitmap GetNameImage(string name, int width = 100, int height = 100)
  39. {
  40. var color = GetNameColor(name);//获取颜色
  41. var nameLength = name.Length;//获取姓名长度
  42. var nameWritten = name;//需要绘制的文字
  43. if (nameLength > 2)//如果名字长度超过2个
  44. {
  45. // 如果用户输入的姓名大于等于3个字符,截取后面两位
  46. var firstName = name.Substring(0, 1);
  47. if (IsChinese(firstName))
  48. {
  49. // 截取倒数两位汉字
  50. nameWritten = name.Substring(name.Length - 2);
  51. }
  52. else
  53. {
  54. // 截取前面的两个英文字母
  55. nameWritten = name.Substring(0, 2).ToUpper();
  56. }
  57. }
  58. var bmp = new SKBitmap(width, height);
  59. using (var canvas = new SKCanvas(bmp))
  60. {
  61. canvas.DrawColor(SKColor.Parse(color));
  62. using (var sKPaint = new SKPaint())
  63. {
  64. sKPaint.Color = SKColors.White;//字体颜色
  65. sKPaint.TextSize = 25;//字体大小
  66. sKPaint.IsAntialias = true;//开启抗锯齿
  67. sKPaint.Typeface = SKTypeface.FromFamilyName("微软雅黑");//字体
  68. var size = new SKRect();
  69. sKPaint.MeasureText(nameWritten, ref size);//计算文字宽度以及高度
  70. var temp = (bmp.Width - size.Size.Width) / 2;
  71. var temp1 = (bmp.Height - size.Size.Height) / 2;
  72. canvas.DrawText(nameWritten, temp, temp1 - size.Top, sKPaint);//画文字
  73. }
  74. }
  75. return bmp;
  76. }
  77. /// <summary>
  78. /// 获取图片base64格式
  79. /// </summary>
  80. /// <param name="name">名称</param>
  81. /// <param name="width">宽</param>
  82. /// <param name="height">高</param>
  83. /// <returns></returns>
  84. public static string GetNameImageBase64(string name, int width = 100, int height = 100)
  85. {
  86. var img = GetNameImage(name, width, height);
  87. var imgByte = img.ImgToBase64String();
  88. return "data:image/png;base64," + imgByte;
  89. }
  90. /// <summary>
  91. /// 用 正则表达式 判断字符是不是汉字
  92. /// </summary>
  93. /// <param name="text">待判断字符或字符串</param>
  94. /// <returns>真:是汉字;假:不是</returns>
  95. private static bool IsChinese(string text)
  96. {
  97. return System.Text.RegularExpressions.Regex.IsMatch(text, @"[\u4e00-\u9fbb]");
  98. }
  99. #endregion 姓名生成图片处理
  100. }