随机生成验证码

 

 1  public class GetVerificationCode
 2     {
 3         /// <summary>
 4         /// 生成随机字符串
 5         /// </summary>
 6         /// <param name="codeCount">验证码长度</param>
 7         /// <returns></returns>
 8         public string CreateRandomCode(int codeCount)
 9         {
10             string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,G,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,g,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
11             string[] allCharArray = allChar.Split(',');
12             string randomCode = "";
13             int temp = -1;
14             Random rand = new Random();
15             for (int i = 0; i < codeCount; i++)
16             {
17                 if (temp != -1)
18                 {
19                     rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
20                 }
21                 int t = rand.Next(35);
22                 if (temp == t)
23                 {
24                     return CreateRandomCode(codeCount);
25                 }
26                 temp = t;
27                 randomCode += allCharArray[t];
28             }
29             return randomCode;
30         }
31 
32 
33 
34         /// <summary>
35         /// 创建验证码图片
36         /// </summary>
37         /// <param name="validateCode">验证码无干扰字符串</param>
38         /// <returns></returns>
39         public byte[] CreateValidateGraphic(string validateCode)
40         {
41             Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 22.0), 40);
42             Graphics g = Graphics.FromImage(image);
43             try
44             {
45                 //生成随机生成器
46                 Random random = new Random();
47                 //清空图片背景色
48                 g.Clear(Color.White);
49                 //画图片的干扰线
50                 for (int i = 0; i < 25; i++)
51                 {
52                     int x1 = random.Next(image.Width);
53                     int x2 = random.Next(image.Width);
54                     int y1 = random.Next(image.Height);
55                     int y2 = random.Next(image.Height);
56                     g.DrawLine(new Pen(Color.Silver), x1, x2, y1, y2);
57                 }
58                 Font font = new Font("Arial", 20, (FontStyle.Bold | FontStyle.Italic));
59                 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
60                 g.DrawString(validateCode, font, brush, 3, 2);
61 
62 
63                 //画图片的前景干扰线
64                 for (int i = 0; i < 100; i++)
65                 {
66                     int x = random.Next(image.Width);
67                     int y = random.Next(image.Height);
68                     image.SetPixel(x, y, Color.FromArgb(random.Next()));
69                 }
70                 //画图片的边框线
71                 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
72 
73                 //保存图片数据
74                 MemoryStream stream = new MemoryStream();
75                 image.Save(stream, ImageFormat.Jpeg);
76 
77                 //输出图片流
78                 return stream.ToArray();
79             }
80             finally
81             {
82                 g.Dispose();
83                 image.Dispose();
84             }
85         }
86 
87 
88 
89 
90     }

 

 

后台代码

1    string txtcode = "";
2             GetVerificationCode code = new GetVerificationCode();
3             txtcode = code.CreateRandomCode(6);
4             byte[] buf = code.CreateValidateGraphic(txtcode);
5             return File(buf,"image/Jpeg");

 

posted @ 2018-08-21 15:29  这个问题解决不了  阅读(163)  评论(0)    收藏  举报