项目要加载System.Drawing的NuGet 程序包
public byte[] CreateDefaultHeadImg(string name)
{
byte[] arr = null;
int sizefint = 65;
Stream imgstream = new MemoryStream();
try
{
if (name.Length > 2)
{
if (new Regex("[\u4e00-\u9fa5]", RegexOptions.Compiled).IsMatch(name))
name = name.Remove(0, name.Length - 2);
else
{
sizefint = 100;
name = name.Substring(0, 2);
}
}
Font theFont = new Font("微软雅黑", sizefint, FontStyle.Bold);
var cols = new Color[] {
Color.FromArgb(0, 153, 255),//天蓝
Color.FromArgb(0, 204, 153),//浅绿
Color.FromArgb(0, 51, 153),//深蓝
Color.FromArgb(51, 102, 255),//青色(深)
Color.FromArgb(51, 204, 102),//绿色(普通)
Color.FromArgb(153, 102, 255),//紫色(普通)
Color.FromArgb(102,153,204),//深蓝(淡)
Color.FromArgb(51,204,204),//深蓝(更淡)
Color.FromArgb(153,51,255),//紫色(深)
Color.FromArgb(204,51,102),//淡红
Color.FromArgb(153, 153, 255),//紫色(淡)
Color.FromArgb(102, 51, 51),//红棕
Color.FromArgb(255, 102, 51),//橙色(淡)
Color.FromArgb(102, 51, 102),//紫红
Color.FromArgb(51, 102, 102),//水墨绿
Color.FromArgb(152, 102, 153),//粉紫
Color.FromArgb(255, 153, 51),//土黄(深)
Color.FromArgb(152, 102, 102),//棕粉
Color.FromArgb(255, 153,153),//粉色(深)
Color.FromArgb(204, 0,153),//紫粉(深)
};
Color col = cols[0];//可以再传入编号选择随机颜色
//文字颜色
Color coldef = Color.FromArgb(255, 255, 255);
Brush newBrush = new SolidBrush(coldef);
int int_ImageWidth = 0;
int fontwidth = 110;
if (name.Length > 0)
{
if (name.Length < 4)
{
int_ImageWidth = name.Length * fontwidth;
}
else
{
int_ImageWidth = name.Length * fontwidth / 2;
}
Random newRandom = new Random();
//图高20px
Bitmap theBitmap = new Bitmap(int_ImageWidth, int_ImageWidth);
Graphics theGraphics = Graphics.FromImage(theBitmap);
SizeF ziSizeF = new SizeF();
ziSizeF = theGraphics.MeasureString(name, theFont); //获取文字宽高
//背景色
theGraphics.Clear(col);
//10pt的字体分行
string tempName = name; //获取用户名称
int column;//第一行显示字数 (如果用户名字长度=2到3个字显示一行,大于三个字显示2行)
if (name.Length > 2)
column = 2;
else
column = name.Length;
//利用循环,来依次输出
for (int i = 0, j = 0; i < tempName.Length; i += column, j++)
{
if (j == 0)
{
string s = tempName.Substring(i, column);
theGraphics.DrawString(s, theFont, newBrush, (int_ImageWidth - ziSizeF.Width) / 2,
(int_ImageWidth - ziSizeF.Height) / 2 + 10);
}
else if (j == 1)
{
string s = tempName.Substring(i, tempName.Length - column);
theGraphics.DrawString(s, theFont, newBrush, (int_ImageWidth - ziSizeF.Width / 2) / 2 - 6,
int_ImageWidth / 2 + (int_ImageWidth / 2 - ziSizeF.Height) / 2);
}
}
Bitmap bitmap = new Bitmap(80, 80); //新建bmp图片
Graphics g = Graphics.FromImage(bitmap); //新建画板
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //制定高质量插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置高质量、低速度呈现平滑程度
g.Clear(Color.White); //清空画布
//在制定位置画图
g.DrawImage(theBitmap, new Rectangle(0, 0, 80, 80),
new Rectangle(0, 0, int_ImageWidth, int_ImageWidth),
GraphicsUnit.Pixel);
//图片转换成流
bitmap.Save(imgstream, System.Drawing.Imaging.ImageFormat.Png);
theGraphics.Dispose();
theBitmap.Dispose();
arr = new byte[imgstream.Length];
imgstream.Position = 0;
imgstream.Read(arr, 0, (int)imgstream.Length);
imgstream.Close();
}
}
catch { }
return arr;
}