根据字符串生成背景,应用于【默认头像】功能

项目要加载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;
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值