平安校园
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.

AvatarUtil.cs 3.5 KiB

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