ASP.NET驗證碼製作

來源:互聯網
上載者:User
這篇文章主要為大家詳細介紹了一個簡單的ASP.NET驗證碼,具有一定的參考價值,感興趣的小夥伴們可以參考一下

本文執行個體為大家分享了ASP.NET驗證碼的具體代碼,供大家參考,具體內容如下

我主要是看到幹擾線了,一個驗證碼裡面要是沒有幹擾線什麼的,至少得在噪點和隨機碼的排版上下工夫:


 /// <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 = { '0', '1', '2', '3', '4', '5', '6', '8', '9' };   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;   }  } }

基本驗證產生代碼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[] { ',' });  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; }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.