Home>Article>Backend Development> Simple production of asp.net verification code

Simple production of asp.net verification code

高洛峰
高洛峰 Original
2017-01-13 15:30:58 1066browse

In fact, there are already many articles about the production of asp.net verification code, but today I still want to share it with you. Dear, you can combine several examples to write an ASP.NET verification code suitable for your own website, probably There are only two parts:

First create an asp.net form ValidateCode.aspx; do not write anything. Write the following code directly in the background ValidateCode.aspx.cs:

protected void Page_Load(object sender, EventArgs e) { string validateCode = CreateValidateCode();//生成验证码 Bitmap bitmap = new Bitmap(imgWidth,imgHeight);//生成Bitmap图像 DisturbBitmap(bitmap); //图像背景 DrewValidateCode(bitmap,validateCode);//绘制验证码图像 bitmap.Save(Response.OutputStream,ImageFormat.Gif);//保存图像,等待输出 } private int codeLen = 4;//验证码长度 private int fineness = 85;//图片清晰度 private int imgWidth = 48;//图片宽度 private int imgHeight = 24;//图片高度 private string fontFamily = "Times New Roman";//字体名称 private int fontSize = 14;//字体大小 //private int fontStyle = 0;//字体样式 private int posX = 0;//绘制起始坐标X private int posY = 0;//绘制坐标Y private string CreateValidateCode() //生成验证码 { string validateCode = ""; Random random = new Random();// 随机数对象 for (int i = 0; i < codeLen; i++)//循环生成每位数值 { int n = random.Next(10);//数字 validateCode += n.ToString(); } Session["vcode"] = validateCode;//保存验证码 这Session是在前台调用的。 return validateCode;// 返回验证码 } private void DisturbBitmap(Bitmap bitmap)//图像背景 { Random random = new Random();//通过随机数生成 for (int i = 0; i < bitmap.Width; i++)//通过循环嵌套,逐个像素点生成 { for (int j = 0; j < bitmap.Height; j++) { if (random.Next(90) <= this.fineness) bitmap.SetPixel(i, j, Color.LightGray); } } } private void DrewValidateCode(Bitmap bitmap, string validateCode)//绘制验证码图像 { Graphics g = Graphics.FromImage(bitmap);//获取绘制器对象 Font font = new Font(fontFamily, fontSize, FontStyle.Bold);//设置绘制字体 g.DrawString(validateCode, font, Brushes.Black, posX, posY);//绘制验证码图像 }

Login.aspx form front desk:

//这个函数是在点击验证码图片就会更换验证码 //可以使用微软自带的jqury.js 下面jquery-1.4.1.min.js版本之上的。或者在jquery官网上下载就可以。  function f_refreshtype() { var Image1 = document.getElementByIdx_x_x_x("img"); if (Image1 != null) { Image1.src = Image1.src + "?"; } } ---//调用函数,实现更换验证码

Backend code: Click login to verify whether the user input is correct .

string usercode = txtcode.Text.Trim(); if (usercode == Session["vcode"].ToString())//Session["vcode"] { }

Other codes are the same as others.

The above is the process of generating ASP.NET verification code shared with you. I hope you can apply what you have learned.

For more articles related to the simple production of asp.net verification codes, please pay attention to 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