Home  >  Article  >  Backend Development  >  ASP.NET verification code production

ASP.NET verification code production

巴扎黑
巴扎黑Original
2017-08-08 11:47:001567browse

This article mainly introduces a simple ASP.NET verification code in detail, which has certain reference value. Interested friends can refer to it

The example in this article shares ASP with everyone. The specific code of NET verification code is for your reference. The specific content is as follows

I mainly see interference lines. If there are no interference lines in a verification code, at least it must be in the layout of noise and random codes. Work hard:


 /// <summary>
 /// 验证码生成类
 /// </summary>
 public class verify_code : IHttpHandler, IRequiresSessionState
 {
  public void ProcessRequest(HttpContext context)
  {
   int codeW = 80;
   int codeH = 22;
   int fontSize = 16;
   string chkCode = string.Empty;
   //颜色列表,用于验证码、噪线、噪点 
   Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
   //字体列表,用于验证码 
   string[] font = { "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" };
   //验证码的字符集,去掉了一些容易混淆的字符 
   char[] character = { &#39;0&#39;, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;8&#39;, &#39;9&#39; };
   Random rnd = new Random();
   //生成验证码字符串 
   for (int i = 0; i < 4; i++)
   {
    chkCode += character[rnd.Next(character.Length)];
   }
   //写入Session
   context.Session["sys_verify_code"] = chkCode;
   //创建画布
   Bitmap bmp = new Bitmap(codeW, codeH);
   Graphics g = Graphics.FromImage(bmp);
   g.Clear(Color.White);
   //画噪线 
   for (int i = 0; i < 4; i++)
   {
    int x1 = rnd.Next(codeW);
    int y1 = rnd.Next(codeH);
    int x2 = rnd.Next(codeW);
    int y2 = rnd.Next(codeH);
    Color clr = color[rnd.Next(color.Length)];
    g.DrawLine(new Pen(clr), x1, y1, x2, y2);
   }
   //画验证码字符串 
   for (int i = 0; i < chkCode.Length; i++)
   {
    string fnt = font[rnd.Next(font.Length)];
    Font ft = new Font(fnt, fontSize);
    Color clr = color[rnd.Next(color.Length)];
    g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18 + 2, (float)0);
   }
   //画噪点 
   for (int i = 0; i < 100; i++)
   {
    int x = rnd.Next(bmp.Width);
    int y = rnd.Next(bmp.Height);
    Color clr = color[rnd.Next(color.Length)];
    bmp.SetPixel(x, y, clr);
   }
   //清除该页输出缓存,设置该页无缓存 
   context.Response.Buffer = true;
   context.Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
   context.Response.Expires = 0;
   context.Response.CacheControl = "no-cache";
   context.Response.AppendHeader("Pragma", "No-Cache");
   //将验证码图片写入内存流,并将其以 "image/Png" 格式输出 
   MemoryStream ms = new MemoryStream();
   try
   {
    bmp.Save(ms, ImageFormat.Png);
    context.Response.ClearContent();
    context.Response.ContentType = "image/Png";
    context.Response.BinaryWrite(ms.ToArray());
   }
   finally
   {
    //显式释放资源 
    bmp.Dispose();
    g.Dispose();
   }
  }

  public bool IsReusable
  {
   get
   {
    return false;
   }
  }
 }

Basic verification generation code demo:


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;

public partial class image : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
  string tmp = RndNum(4);
  HttpCookie a = new HttpCookie("ImageV", tmp);
  Response.Cookies.Add(a);
  this.ValidateCode(tmp);
 }

 private void ValidateCode(string VNum)
 {
  Bitmap Img = null;
  Graphics g = null;
  MemoryStream ms = null;
  int gheight = VNum.Length * 12;
  Img = new Bitmap(gheight, 25);
  g = Graphics.FromImage(Img);
  //背景颜色
  g.Clear(Color.White);
  //文字字体
  Font f = new Font("Arial Black", 10);
  //文字颜色
  SolidBrush s = new SolidBrush(Color.Black);
  g.DrawString(VNum, f, s, 3, 3);
  ms = new MemoryStream();
  Img.Save(ms, ImageFormat.Jpeg);
  Response.ClearContent();
  Response.ContentType = "image/Jpeg";
  Response.BinaryWrite(ms.ToArray());

  g.Dispose();
  Img.Dispose();
  Response.End();
 }

 private string RndNum(int VcodeNum)
 {
  string Vchar = "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,v,w,x,y,z";
  string[] VcArray = Vchar.Split(new Char[] { &#39;,&#39; });
  string VNum = "";
  int temp = -1;

  Random rand = new Random();

  for (int i = 1; i < VcodeNum + 1; i++)
  {
   if (temp != -1)
   {
    rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
   }

   int t = rand.Next(35);
   if (temp != -1 && temp == t)
   {
    return RndNum(VcodeNum);
   }
   temp = t;
   VNum += VcArray[t];
  }
  return VNum;
 }

}

The above is the detailed content of ASP.NET verification code production. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn