C#生成图形验证码

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

LoginController.cs:

1         public ActionResult VerificationCode()
2         {
3             string oldcode = TempData["VerificationCode"] as string;
4             string code = VerificationCodeHelper.CreateRandomCode(4); //验证码的字符为4个
5             TempData["VerificationCode"] = code; //验证码存放在TempData中
6             return File(VerificationCodeHelper.CreateValidateGraphic(code), "image/Jpeg");
7         }
LoginController

使用:

<img src="/Login/VerificationCode" onclick="this.src=this.src+'?'"/>

猜你喜欢

转载自www.cnblogs.com/shangec/p/13367896.html