c# asp.net实现验证码

建立ValidateImg.aspx文件,里面代码如下:

 

  1. <%@ Page Language="c#" %>
  2. <%@ Import Namespace="System.Web" %>
  3. <%@ Import Namespace="System.Drawing" %>
  4. <%@ Import Namespace="System.Drawing.Imaging" %>
  5. <script runat="server" language="C#">
  6.     void Page_Load(Object sender, EventArgs e)
  7.     {
  8.         int charsNo = 4;
  9.         int fontSize = 12;
  10.         int bgWidth = 60;
  11.         int bgHeight = 20;
  12.         float x = (bgWidth - (charsNo * (fontSize + 0.5F))) / 2; // TODO: optimize
  13.         float y = (bgHeight - (fontSize * 1.7F)) / 2; // TODO: optimize
  14.         // Generate the text
  15.         //
  16.         //string genText = Globals.CreateTemporaryPassword(charsNo).ToUpper();
  17.         string genText = CreateTemporaryPassword(charsNo).ToUpper();
  18.         // Add the generate text to a session variable 
  19.         //
  20.         Session.Add("ValidateNumber", genText);
  21.         // Create the memory map 
  22.         //
  23.         Bitmap raster;
  24.         // Select an memory image from file of 290x80 px
  25.         // in the current dir, NoSpamBgImgs folder named bg_X.jpg
  26.         //
  27.         Graphics graphicObj;
  28.         string bgFilePath = Server.MapPath(@"./AntiSpamBgImgs/bg_" + new Random().Next(1) + ".jpg");
  29.         System.Drawing.Image imgObj = System.Drawing.Image.FromFile(bgFilePath);
  30.         // Creating the raster image & graphic objects
  31.         //
  32.         raster = new Bitmap(imgObj, bgWidth, bgHeight);
  33.         graphicObj = Graphics.FromImage(raster);
  34.         // Creating an array for most readable yet cryptic fonts for OCR's
  35.         // This is entirely up to developer's discretion
  36.         // CAPTCHA recomandation
  37.         //
  38.         String[] crypticFonts = new String[13];
  39.         crypticFonts[0] = "Arial";
  40.         crypticFonts[1] = "Verdana";
  41.         crypticFonts[2] = "Comic Sans MS";
  42.         crypticFonts[3] = "Impact";
  43.         crypticFonts[4] = "Haettenschweiler";
  44.         crypticFonts[5] = "Lucida Sans Unicode";
  45.         crypticFonts[6] = "Garamond";
  46.         crypticFonts[7] = "Courier New";
  47.         crypticFonts[8] = "Book Antiqua";
  48.         crypticFonts[9] = "Arial Narrow";
  49.         crypticFonts[10] = "Fixedsys";
  50.         crypticFonts[11] = "宋体";
  51.         crypticFonts[12] = "仿体";
  52.         // Instantiate object of brush with black color
  53.         //
  54.         Random r = new Random();
  55.         SolidBrush brushes = new SolidBrush(Color.FromArgb(r.Next(150), r.Next(150), r.Next(150)));
  56.         // Loop to write the characters on image with different fonts
  57.         // CAPTCHA method
  58.         //
  59.         for (int a = 0; a < genText.Length; a++)
  60.         {
  61.             Font fontObj = new Font(crypticFonts[r.Next(crypticFonts.Length)], fontSize);
  62.             graphicObj.DrawString(genText.Substring(a, 1), fontObj, brushes, x + (a * fontSize), y);
  63.         }
  64.         // Flush again
  65.         //
  66.         graphicObj.Flush();
  67.         // Setting the response header content MIME type
  68.         //
  69.         Response.ContentType = "image/gif";
  70.         // Saving the file to output stream to be displayed in browser
  71.         //
  72.         raster.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
  73.         // Flushing to the Response 
  74.         //
  75.         Response.Flush();
  76.     }
  77.     public static String CreateTemporaryPassword(int length)
  78.     {
  79.         string str = "";
  80.         Random r = new Random();
  81.         for (int i = 0; i < length; i++)
  82.         {
  83.             str += r.Next(0, 9).ToString();
  84.         }
  85.         return str;
  86.     }
  87. </script> 

使用方法:
 

 <img src="/ValidateImg.aspx" align="absmiddle" id="code" width="66" height="30" alt="验证码" /><a href="javascript:document.getElementById('code').src='/ValidateImg.aspx?'+Math.random();void(0);">看不清楚,换一个验证码</a>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值