加入收藏 | 设为首页 | 会员中心 | 我要投稿 航空爱好网 (https://www.52kongjun.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

c 语言实现 图形验证码,一个完整asp.net图形验证码程序

发布时间:2023-01-05 05:02:42 所属栏目:Asp教程 来源:互联网
导读: 1、测试页面:showcode.aspx
CodeFile="showcode.aspx.cs" Inherits="showcode"
%>
/p>
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
>
ASP.NET验证码

1、测试页面:showcode.aspx

CodeFile="showcode.aspx.cs" Inherits="showcode"

%>

/p>

Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

>

ASP.NET验证码

runat="server">

请输入验证码:

runat="server" maxlength="5" size="5"/>

ImageUrl="verifyimg.aspx" ImageAlign="Middle"

/>

ID="RequiredFieldValidator1" runat="server" ErrorMessage="您没有输入验证码"

ControlToValidate="txtVI">

/>

OnClick="Button1_Click"

/>

2、测试页面后台:showcode.aspx.cs

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Configuration;

using System.Web.Security;

//using System.Web.UI.WebControls.WebParts;

namespace yjsjy

{

///

/// showcode 的摘要说明。

///

public class showcode : System.Web.UI.Page

{

protected

System.Web.UI.WebControls.TextBox TextBox1;

protected

System.Web.UI.WebControls.Image Image1;

protected

System.Web.UI.WebControls.Button Button1;

protected

System.Web.UI.WebControls.Label Label1;

private void Page_Load(object

sender, System.EventArgs e)

{

//

在此处放置用户代码以初始化页面

}

#region Web 窗体设计器生成的代码

override protected void

OnInit(EventArgs e)

{

//

// CODEGEN:

该调用是 ASP.NET Web 窗体设计器所必需的。

//

InitializeComponent();

base.OnInit(e);

}

///

/// 设计器支持所需的方法 -

不要使用代码编辑器修改

/// 此方法的内容。

///

private void

InitializeComponent()

{this.Button1.Click

+= new System.EventHandler(this.Button1_Click);

this.Load +=

new System.EventHandler(this.Page_Load);

}

#endregion

private void

Button1_Click(object sender, System.EventArgs e)

{

if

(string.Compare(Session["code"].ToString(), this.TextBox1.Text,

true) == 0)

{

this.Label1.Text

= "成功";

}

else

{

this.Label1.Text

= "失败";

return;

}

}

}

}

3、生成验证码页面:verifyimg.aspx

CodeFile="verifyimg.aspx.cs"

Inherits="verifyimg" %>

4、生成验证码页面后台:verifyimg.aspx.cs

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Configuration;

using System.Web.Security;

//using System.Web.UI.WebControls.WebParts;

using System.IO;

using System.Drawing.Imaging;

using System.Drawing.Drawing2D;

using System.Drawing.Printing;

namespace yjsjy

{

///

/// verifyimg 的摘要说明。

///

public class verifyimg : System.Web.UI.Page

{

private void Page_Load(object

sender, System.EventArgs e)

{

//

在此处放置用户代码以初始化页面

CreateCheckCodeImage(GenerateCheckCode());

}

///

/// 生成随机校验码字符串

///

///

生成的随机校验码字符串

private string

GenerateCheckCode()

{

int

number;

string

strCode = string.Empty;

//随机数种子

Random random

= new Random();

for

(int i = 0; i < 4; i++) //校验码长度为4

{

//随机的整数

number

= random.Next();

//字符从0-9,A-Z中随机产生,对应的ASCII码分别为

//48-57,65-90

number

= number % 36;

if

(number < 10)

{

number

+= 48;

}

else

{

number

+= 55;

}

strCode

+= ((char)number).ToString();

}

//在session中保存校验码

Session["code"]

= strCode;

return

strCode;

}

///

/// 根据校验码输出图片

///

///

name="checkCode">产生的随机校验码

private void

CreateCheckCodeImage(string checkCode)

{

//若校验码为空,则直接返回

if (checkCode

== null || checkCode.Trim() == String.Empty)

{

return;

}

//根据校验码的长度确定输出图片的长度

System.Drawing.Bitmap

image = new System.Drawing.Bitmap(55,

20);//(int)Math.Ceiling(Convert.ToDouble(checkCode.Length *

15))

//创建Graphics对象

Graphics g =

Graphics.FromImage(image);

try

{

//生成随机数种子

Random

random = new Random();

//清空图片背景色

g.Clear(Color.White);

//画图片的背景噪音线

10条

//---------------------------------------------------

for

(int i = 0; i < 10; i++)

{

//噪音线起点坐标(x1,y1),终点坐标(x2,y2)

int

x1 = random.Next(image.Width);

int

x2 = random.Next(image.Width);

int

y1 = random.Next(image.Height);

int

y2 = random.Next(image.Height);

//用银色画出噪音线

g.DrawLine(new

Pen(Color.Silver), x1, y1, x2, y2);

}

//---------------------------------------------------

//Brush

b = Brushes.Silver;

//g.FillRectangle(b,

0, 0, image.Width, image.Height);

//---------------------以上两种任选其一------------------------------

//输出图片中校验码的字体:

12号Arial,粗斜体

Font

font = new Font("Arial",12,(FontStyle.Bold |

FontStyle.Italic));

//线性渐变画刷

LinearGradientBrush

brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width,

image.Height),Color.Blue,Color.Purple,1.2f,true);

g.DrawString(checkCode,

font, brush, 2, 2);

//画图片的前景噪音点

50个

for

(int i = 0; i < 50; i++)

{

int

x = random.Next(image.Width);

int

y = random.Next(image.Height);

image.SetPixel(x,

y, Color.FromArgb(random.Next()));

}

//画图片的边框线

g.DrawRectangle(new

Pen(Color.Peru),0,0,image.Width - 1,image.Height - 1);

//创建内存流用于输出图片

using

(MemoryStream ms = new MemoryStream())

{

//图片格式指定为png

image.Save(ms,

ImageFormat.Jpeg);

//清除缓冲区流中的所有输出

Response.ClearContent();

(编辑:航空爱好网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!