asp.net 验证码功能

 1  public FileContentResult CreateImageCode()
 2         {
 3             string code = CreateVerifyCode();
 4 
 5             Session.SetCaptchaCode(code.ToUpper());//验证码存入Session中
 6             int fSize = FontSize;
 7             int fWidth = fSize + Padding;
 8             int imageWidth = (int)(code.Length * fWidth) + 4 + Padding * 2;
 9             int imageHeight = fSize * 2 + Padding;
10 
11             Bitmap image = new Bitmap(imageWidth, imageHeight);
12             Graphics g = Graphics.FromImage(image);
13             g.Clear(BackgroundColor);
14             Random rand = new Random();
15 
16             //给背景添加随机生成的燥点
17             if (this.Chaos)
18             {
19                 Pen pen = new Pen(ChaosColor, 0);
20                 int c = Length * 10;
21 
22                 for (int i = 0; i < c; i++)
23                 {
24                     int x = rand.Next(image.Width);
25                     int y = rand.Next(image.Height);
26 
27                     g.DrawRectangle(pen, x, y, 1, 1);
28                 }
29             }
30 
31             int left = 0, top = 0, top1 = 1, top2 = 1;
32 
33             int n1 = (imageHeight - FontSize - Padding * 2);
34             int n2 = n1 / 4;
35             top1 = n2;
36             top2 = n2 * 2;
37 
38             Font f;
39             Brush b;
40             int cindex, findex;
41 
42             //随机字体和颜色的验证码字符
43             for (int i = 0; i < code.Length; i++)
44             {
45                 cindex = rand.Next(Colors.Length - 1);
46                 findex = rand.Next(Fonts.Length - 1);
47 
48                 f = new Font(Fonts[findex], fSize, FontStyle.Bold);
49                 b = new SolidBrush(Colors[cindex]);
50 
51                 if (i % 2 == 1)
52                 {
53                     top = top2;
54                 }
55                 else
56                 {
57                     top = top1;
58                 }
59 
60                 left = i * fWidth;
61 
62                 g.DrawString(code.Substring(i, 1), f, b, left, top);
63             }
64 
65             //画一个边框 边框颜色为Color.Gainsboro
66             g.DrawRectangle(new Pen(Color.Gainsboro, 0), 0, 0, image.Width - 1, image.Height - 1);
67             g.Dispose();
68 
69             //产生波形
70             image = TwistImage(image, true, 0, 4);
71             MemoryStream ms = new MemoryStream();
72             //将图像保存到指定的流
73             image.Save(ms, ImageFormat.Jpeg);
74             return File(ms.GetBuffer(), "image/JPEG");
75         }
1   int length = 4;
2         public int Length
3         {
4             get { return length; }
5             set { length = value; }
6         }
1   int fontSize = 25;
2         public int FontSize
3         {
4             get { return fontSize; }
5             set { fontSize = value; }
6         }
1  int padding = 2;
2         public int Padding
3         {
4             get { return padding; }
5             set { padding = value; }
6         }
1  bool chaos = false;
2         public bool Chaos
3         {
4             get { return chaos; }
5             set { chaos = value; }
6         }
1  Color chaosColor = Color.LightGray;
2         public Color ChaosColor
3         {
4             get { return chaosColor; }
5             set { chaosColor = value; }
6         }
1  Color backgroundColor = Color.White;
2         public Color BackgroundColor
3         {
4             get { return backgroundColor; }
5             set { backgroundColor = value; }
6         }
1  Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
2         public Color[] Colors
3         {
4             get { return colors; }
5             set { colors = value; }
6         }
1  string[] fonts = { "Arial", "Georgia" };
2         public string[] Fonts
3         {
4             get { return fonts; }
5             set { fonts = value; }
6         }
1  string codeSerial = "1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";
2         public string CodeSerial
3         {
4             get { return codeSerial; }
5             set { codeSerial = value; }
6         }
 1   private const double PI = 3.1415926535897932384626433832795;
 2         private const double PI2 = 6.283185307179586476925286766559;
 3 
 4         /// <summary>
 5         /// 正弦曲线Wave扭曲图片(Edit By 51aspx.com)
 6         /// </summary>
 7         /// <param name="srcBmp">图片路径</param>
 8         /// <param name="bXDir">如果扭曲则选择为True</param>
 9         /// <param name="nMultValue">波形的幅度倍数,越大扭曲的程度越高,一般为3</param>
10         /// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>
11         /// <returns></returns>
12         public Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
13         {
14             Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
15 
16             // 将位图背景填充为白色
17             Graphics graph = Graphics.FromImage(destBmp);
18             graph.FillRectangle(new SolidBrush(Color.White), 0, 0, destBmp.Width, destBmp.Height);
19             graph.Dispose();
20 
21             double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
22 
23             for (int i = 0; i < destBmp.Width; i++)
24             {
25                 for (int j = 0; j < destBmp.Height; j++)
26                 {
27                     double dx = 0;
28                     dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
29                     dx += dPhase;
30                     double dy = Math.Sin(dx);
31 
32                     // 取得当前点的颜色
33                     int nOldX = 0, nOldY = 0;
34                     nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
35                     nOldY = bXDir ? j : j + (int)(dy * dMultValue);
36 
37                     Color color = srcBmp.GetPixel(i, j);
38                     if (nOldX >= 0 && nOldX < destBmp.Width
39                      && nOldY >= 0 && nOldY < destBmp.Height)
40                     {
41                         destBmp.SetPixel(nOldX, nOldY, color);
42                     }
43                 }
44             }
45 
46             return destBmp;
47         }
 1  public string CreateVerifyCode(int codeLen = 4)
 2         {
 3             string[] arr = CodeSerial.Split(',');
 4 
 5             string code = string.Empty;
 6 
 7             int randValue = -1;
 8 
 9             Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
10 
11             for (int i = 0; i < codeLen; i++)
12             {
13                 randValue = rand.Next(0, arr.Length - 1);
14 
15                 code += arr[randValue];
16             }
17 
18             return code;
19         }

 

posted @ 2016-09-08 10:33  夏狗蛋  阅读(58)  评论(0)    收藏  举报