在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1.新建一个页面,ImageCode.aspx 2.在Page_Load中添加如下代码 string tmp = RndNum(4); 3.在该页面中添加两个方法 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); //生成随机生成器 Random random = new Random(); //背景颜色 for (int i = 0; i < 100; i++) int x = random.Next(Img.Width); int y = random.Next(Img.Height); Img.SetPixel(x, y, Color.FromArgb(random.Next())); }
//文字颜色 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(); } 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 = " "; for (int i = 1; i < VcodeNum + 1; i++) } } } return VNum; } 4.在需要显示验证码的页面添加一个图片控件 5.在该页的前台页面添加如下代码: 验证码: 6.在该页的Page_Load事件中添加如下代码: ibtn_yzm.ImageUrl = "ImageCode.aspx"; 7.验证该验证码是否输入正确的代码: string code = tbx_yzm.Text; 8.效果 |
请发表评论