C# 随机生成名字,电话,图像

  1 随机生成3个字的名字 【可直接看效果图】

public static List<string> GetTels(int count)
 {
           List<string> tels = new List<string>() { };

           string[] tel1 = new string[] { "130", "187", "156", "179", "188", "199", "180","176","190"};


           Random rad = new Random();//实例化随机数产生器rad;
            
           for (int i = 0; i < count;i++ )
           {
               string tel = tel1[rad.Next(0, tel1.Length - 1)] + StringUtils.FillZero(rad.Next(1000, 10000),4)+StringUtils.FillZero(rad.Next(1000, 10000),4);
               if (tels.Contains(tel) == false)
               {
                   tels.Add(tel);
               }
               else
               {
                   i--;
               }
           }


           return tels;
    
}



public static List<string> GetNames(int count)
{
       
           Random ran = new Random();

           List<string> s = new List<string> { };

           string[] nameS3 = new string[] { "赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯",
                "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许", "何", "吕", "施",
                "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏",
                "水", "窦", "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎" };

           string[] nameS2 = new string[] {"鲁","韦","昌","马","苗","凤","花","方","俞","任","袁"
                  ,"柳","酆","鲍","史","唐","费","廉","岑","薛","雷","贺","倪","汤","滕","殷","罗",
                  "毕","郝","邬","安","常","乐","于","时","傅","皮","卞","齐","康","伍","余","元",
                  "卜","顾","孟","平","黄"};

           string[] nameS1 = new string[] { "梅", "盛", "林", "刁", "锺", "徐", "邱", "骆", "高",
                "夏", "蔡", "樊", "胡", "凌", "霍", "虞", "万", "支", "柯", "昝", "管", "卢", "莫",
                "经", "房", "裘", "缪", "干", "解", "应", "宗", "丁", "宣", "贲", "邓", "郁", "单",
                "杭", "洪", "包", "诸", "左", "石", "崔", "吉", "钮", "龚", "程", "嵇", "邢", "滑",
                "裴", "陆", "荣", "翁", "荀", "羊", "於", "惠", "甄", "麴", "家", "封", "芮", "羿",
                "储", "靳", "汲", "邴", "糜", "松", "井" };

           for (int i = 0; i < count; i++)
           {
               string s1 = nameS1[ran.Next(0, nameS1.Length - 1)];
               string s2 = nameS2[ran.Next(0, nameS2.Length - 1)];
               string s3 = nameS3[ran.Next(0, nameS3.Length - 1)];
               string name = s1 + s2 + s3;
               if (!s.Contains(name))
               {
                   s.Add(name);
               }
               else
               {
                   i--;
               }
           }
           return s;
 }

 随机产生图片 

public string CreateImage(string name)
 {
            int initialWidth = 50, initialHeight = 50;
            Bitmap theBitmap = new Bitmap(initialWidth, initialHeight);
            Graphics theGraphics = Graphics.FromImage(theBitmap);
            //呈现质量
            theGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            //背景色
            theGraphics.Clear(Color.Transparent);
          
            Brush b = ColorUtils.GetOneBrush();
            theGraphics.FillEllipse(b, new Rectangle(0, 0, 50, 50));

            Font font = new Font("宋体", 13, FontStyle.Bold);
            SizeF sizeF = theGraphics.MeasureString(name, font);
             
            theGraphics.DrawString(name + "", font, Brushes.Wheat, (initialWidth - sizeF.Width) / 2, (initialHeight-sizeF.Height)/2);

            string path = "Images\\" + DateTime.Now.ToString("yy_MM_dd") + DateTime.Now.ToString("HH_mm_ss_fff") + ".png";
            theBitmap.Save(path);
            
            return ImageUtils.ImageFileToBase64String(path); ;
  }

效果图:

标题

猜你喜欢

转载自blog.csdn.net/taoerit/article/details/80839739