c#.net编写一个验证码,点击验证码图片可以切换验证码,输入正确或者错误的时候都有文字提示
开发平台是MicrosoftVisualStudio2008ASP.NETweb应用程序。。跪求代码了。网上查了几个,不知道为什么都运行不出来...
开发平台是Microsoft Visual Studio 2008 ASP.NET web应用程序。。跪求代码了。网上查了几个,不知道为什么都运行不出来
展开
4个回答
展开全部
两个页面:
1.ValidateCode用于生成验证图片的
2.ValidateFormPage表单显示的
ValidateCode.aspx文件不用写代码
ValidateCode.aspx.cs代码如下:
using System;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
namespace WebFormDemo
{
public partial class ValidateCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 4位数字的验证码
string str_ValidateCode = GetRandomNumberString(4);
// 用于验证的Session
Session["ValidateCode"] = str_ValidateCode;
CreateImage(str_ValidateCode);
}
}
public string GetRandomNumberString(int int_NumberLength)
{
string str_Number = string.Empty;
Random theRandomNumber = new Random();
for (int int_index = 0; int_index < int_NumberLength; int_index++)
str_Number += theRandomNumber.Next(10).ToString();
return str_Number;
}
//生成随机颜色
public Color GetRandomColor()
{
Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
// 对于C#的随机数,没什么好说的
System.Threading.Thread.Sleep(RandomNum_First.Next(50));
Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
// 为了在白色背景上显示,尽量生成深色
int int_Red = RandomNum_First.Next(256);
int int_Green = RandomNum_Sencond.Next(256);
int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
int_Blue = (int_Blue > 255) ? 255 : int_Blue;
return Color.FromArgb(int_Red, int_Green, int_Blue);
}
//根据验证字符串生成最终图象
public void CreateImage(string str_ValidateCode)
{
int int_ImageWidth = str_ValidateCode.Length * 13;
Random newRandom = new Random();
// 图高20px
Bitmap theBitmap = new Bitmap(int_ImageWidth, 20);
Graphics theGraphics = Graphics.FromImage(theBitmap);
// 白色背景
theGraphics.Clear(Color.White);
// 灰色边框
theGraphics.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, int_ImageWidth - 1, 19);
// 10pt的字体
Font theFont = new Font("Arial", 10);
for (int int_index = 0; int_index < str_ValidateCode.Length; int_index++)
{
string str_char = str_ValidateCode.Substring(int_index, 1);
Brush newBrush = new SolidBrush(GetRandomColor());
Point thePos = new Point(int_index * 13 + 1 + newRandom.Next(3), 1 + newRandom.Next(3));
theGraphics.DrawString(str_char, theFont, newBrush, thePos);
}
// 将生成的图片发回客户端
MemoryStream ms = new MemoryStream();
theBitmap.Save(ms, ImageFormat.Png);
Response.ClearContent(); //需要输出图象信息 要修改HTTP头
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
theGraphics.Dispose();
theBitmap.Dispose();
Response.End();
}
}
}
----------------------------------------------------------------------------
ValidateFormPage.aspx文件代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ValidateFormPage.aspx.cs" Inherits="WebFormDemo.ValidateFormPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="ValidateCode.aspx" /><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label
ID="Label1" runat="server"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="验证" />
</div>
</form>
</body>
</html>
----------------------------------------------------------------------------
ValidateFormPage.aspx.cs文件代码如下:
using System;
namespace WebFormDemo
{
public partial class ValidateFormPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//给按钮加一个点击事件
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == Session["ValidateCode"].ToString())
{
Label1.Text = "正确!";
}
else
{
Label1.Text = "错误!";
}
}
}
}
1.ValidateCode用于生成验证图片的
2.ValidateFormPage表单显示的
ValidateCode.aspx文件不用写代码
ValidateCode.aspx.cs代码如下:
using System;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
namespace WebFormDemo
{
public partial class ValidateCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 4位数字的验证码
string str_ValidateCode = GetRandomNumberString(4);
// 用于验证的Session
Session["ValidateCode"] = str_ValidateCode;
CreateImage(str_ValidateCode);
}
}
public string GetRandomNumberString(int int_NumberLength)
{
string str_Number = string.Empty;
Random theRandomNumber = new Random();
for (int int_index = 0; int_index < int_NumberLength; int_index++)
str_Number += theRandomNumber.Next(10).ToString();
return str_Number;
}
//生成随机颜色
public Color GetRandomColor()
{
Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
// 对于C#的随机数,没什么好说的
System.Threading.Thread.Sleep(RandomNum_First.Next(50));
Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
// 为了在白色背景上显示,尽量生成深色
int int_Red = RandomNum_First.Next(256);
int int_Green = RandomNum_Sencond.Next(256);
int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
int_Blue = (int_Blue > 255) ? 255 : int_Blue;
return Color.FromArgb(int_Red, int_Green, int_Blue);
}
//根据验证字符串生成最终图象
public void CreateImage(string str_ValidateCode)
{
int int_ImageWidth = str_ValidateCode.Length * 13;
Random newRandom = new Random();
// 图高20px
Bitmap theBitmap = new Bitmap(int_ImageWidth, 20);
Graphics theGraphics = Graphics.FromImage(theBitmap);
// 白色背景
theGraphics.Clear(Color.White);
// 灰色边框
theGraphics.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, int_ImageWidth - 1, 19);
// 10pt的字体
Font theFont = new Font("Arial", 10);
for (int int_index = 0; int_index < str_ValidateCode.Length; int_index++)
{
string str_char = str_ValidateCode.Substring(int_index, 1);
Brush newBrush = new SolidBrush(GetRandomColor());
Point thePos = new Point(int_index * 13 + 1 + newRandom.Next(3), 1 + newRandom.Next(3));
theGraphics.DrawString(str_char, theFont, newBrush, thePos);
}
// 将生成的图片发回客户端
MemoryStream ms = new MemoryStream();
theBitmap.Save(ms, ImageFormat.Png);
Response.ClearContent(); //需要输出图象信息 要修改HTTP头
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
theGraphics.Dispose();
theBitmap.Dispose();
Response.End();
}
}
}
----------------------------------------------------------------------------
ValidateFormPage.aspx文件代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ValidateFormPage.aspx.cs" Inherits="WebFormDemo.ValidateFormPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="ValidateCode.aspx" /><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label
ID="Label1" runat="server"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="验证" />
</div>
</form>
</body>
</html>
----------------------------------------------------------------------------
ValidateFormPage.aspx.cs文件代码如下:
using System;
namespace WebFormDemo
{
public partial class ValidateFormPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//给按钮加一个点击事件
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == Session["ValidateCode"].ToString())
{
Label1.Text = "正确!";
}
else
{
Label1.Text = "错误!";
}
}
}
}
展开全部
可以自己在网上找个验证码的代码,然后再页面用<img src="checkimage.aspx" id="yzm" name="yzm"><a href="#" onclick="yzm.src='checkimage.aspx?t='+(new Date().getTime());return false;">看不清,换一张?</a>就可以了
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上有web的 我曾经做过一个winform的 也是类似。代码也差不多。建议采纳楼上
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询