asp.net mvc 验证码

  1 #region  生成验证码图片
  2         //[OutputCache(Location = OutputCacheLocation.None, Duration = 0, NoStore = false)]
  3         public ActionResult SecurityCode()
  4         {
  5 
  6             string oldcode = TempData["SecurityCode"] as string;
  7             string code = CreateRandomCode(5);
  8             TempData["SecurityCode"] = code;
  9             return File(CreateValidateGraphic(code), "image/Jpeg");
 10         }
 11 
 12 
 13         private byte[] CreateImage(string checkCode)
 14         {
 15             int iwidth = (int)(checkCode.Length * 12);
 16             System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
 17             Graphics g = Graphics.FromImage(image);
 18             Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
 19             Brush b = new System.Drawing.SolidBrush(Color.White);
 20             g.Clear(Color.Blue);
 21             g.DrawString(checkCode, f, b, 3, 3);
 22             Pen blackPen = new Pen(Color.Black, 0);
 23             Random rand = new Random();
 24             for (int i = 0; i < 5; i++)
 25             {
 26                 int x1 = rand.Next(image.Width);
 27                 int x2 = rand.Next(image.Width);
 28                 int y1 = rand.Next(image.Height);
 29                 int y2 = rand.Next(image.Height);
 30                 g.DrawLine(new Pen(Color.Silver, 1), x1, y1, x2, y2);
 31             }
 32             System.IO.MemoryStream ms = new System.IO.MemoryStream();
 33             image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
 34             return ms.ToArray();
 35         }
 36 
 37         private string CreateRandomCode(int codeCount)
 38         {
 39             string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
 40             string[] allCharArray = allChar.Split(,);
 41             string randomCode = "";
 42             int temp = -1;
 43             Random rand = new Random();
 44             for (int i = 0; i < codeCount; i++)
 45             {
 46                 if (temp != -1)
 47                 {
 48                     rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
 49                 }
 50                 int t = rand.Next(35);
 51                 if (temp == t)
 52                 {
 53                     return CreateRandomCode(codeCount);
 54                 }
 55                 temp = t;
 56                 randomCode += allCharArray[t];
 57             }
 58             return randomCode;
 59         }
 60         /// <summary>
 61         /// 创建验证码的图片
 62         /// </summary>
 63         public byte[] CreateValidateGraphic(string validateCode)
 64         {
 65             Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 16.0), 27);
 66             Graphics g = Graphics.FromImage(image);
 67             try
 68             {
 69                 //生成随机生成器
 70                 Random random = new Random();
 71                 //清空图片背景色
 72                 g.Clear(Color.White);
 73                 //画图片的干扰线
 74                 for (int i = 0; i < 25; i++)
 75                 {
 76                     int x1 = random.Next(image.Width);
 77                     int x2 = random.Next(image.Width);
 78                     int y1 = random.Next(image.Height);
 79                     int y2 = random.Next(image.Height);
 80                     g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
 81                 }
 82                 Font font = new Font("Arial", 13, (FontStyle.Bold | FontStyle.Italic));
 83                 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
 84                  Color.Blue, Color.DarkRed, 1.2f, true);
 85                 g.DrawString(validateCode, font, brush, 3, 2);
 86                 //画图片的前景干扰点
 87                 for (int i = 0; i < 100; i++)
 88                 {
 89                     int x = random.Next(image.Width);
 90                     int y = random.Next(image.Height);
 91                     image.SetPixel(x, y, Color.FromArgb(random.Next()));
 92                 }
 93                 //画图片的边框线
 94                 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
 95                 //保存图片数据
 96                 MemoryStream stream = new MemoryStream();
 97                 image.Save(stream, ImageFormat.Jpeg);
 98                 //输出图片流
 99                 return stream.ToArray();
100             }
101             finally
102             {
103                 g.Dispose();
104                 image.Dispose();
105             }
106         }
107         #endregion

 

asp.net mvc 验证码,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。