在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Drawing" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!-- 1.准备Bitmap对象 image: System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20); 2.利用Graphics对象g在上面画图: Graphics g = Graphics.FromImage(image); g.DrawString(checkCode, f, b, 3, 3); 3.然后Response.BinaryWrite输出: Response.BinaryWrite(ms.ToArray()); 优化想法: 将验证码以js脚本可读取的方式发送到客户端; 客户端先出发js脚本的验证,通过后才允许提交; 提供重新获取验证码功能。 --> <script runat="server"> /// <summary> /// 输入要求的验证码的位数,返回一个验证码字符串 /// </summary> /// <param name="codeCount">验证码的位数</param> /// <returns>验证码字符串</returns> private string CreateRandomCode(int codeCount) { 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,V,W,X,Y,Z," + "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[] allCharArray = allChar.Split(','); int allCharLen = allCharArray.Length; string randomCode = ""; Random rand = new Random(); for (int i = 0; i < codeCount; i++) { randomCode += allCharArray[rand.Next(0, allCharLen)]; } return randomCode; } /// <summary> /// 根据验证码checkCode画图 /// </summary> /// <param name="checkCode"></param> private void CreateImage(string checkCode) { int iwidth = (int)(checkCode.Length * 11.5); System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20); Graphics g = Graphics.FromImage(image); Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold); Brush b = new System.Drawing.SolidBrush(Color.White); //g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height); g.Clear(Color.Blue); g.DrawString(checkCode, f, b, 3, 3); g.Dispose(); //Pen blackPen = new Pen(Color.Black, 0); //Random rand = new Random(); //for (int i = 0; i < 5; i++) //{ // int y = rand.Next(image.Height); // g.DrawLine(blackPen, 0, y, image.Width, y); //} System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); Response.ClearContent(); Response.ContentType = "image/Jpeg"; Response.BinaryWrite(ms.ToArray()); image.Dispose(); //---------缓存验证码,以备验证--------- Session["ValidateCode"] = checkCode; /*如果希望在挂接该aspx页的页面A.aspx内执行Session的绑定就是白扯! * A页面首先绑定,然后是确定表达式,此时B.aspx未执行,还没有产生Session呢! * */ } protected void Page_Load(object sender, EventArgs e) { CreateImage(CreateRandomCode(6)); } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>无标题页</title> </head> <body> </body> </html> 调用上面的验证程序:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void btnSend_Click(object sender, EventArgs e) { if(Session["ValidateCode"].ToString()!=txtValidateCode.Text) Response.Write("验证码不正确"); else Response.Write("登录到新的界面!"); } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtValidateCode" runat="server"></asp:TextBox> <img src="ValidateCode.aspx" alt="验证码" /> <br /> <asp:Button ID="btnSend" runat="server" OnClick="btnSend_Click" Text="发送" /></div> </form> </body> </html> |
请发表评论