asp.net如何实现验证码

hyj122 2005-09-13 03:29:24
asp.net如何实现验证码
asp.net如何实现验证码
asp.net如何实现验证码
asp.net如何实现验证码
谢谢!!!!
...全文
264 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaowangtian1117 2005-09-13
  • 打赏
  • 举报
回复
/* Copyright all(c) 2005 ZhongFeng, http://blog.csdn.net/SW515 */
public class ValidateCode : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
this.CreateCheckCodeImage(GenerateCheckCode());
}
#region web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 asp.NET web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for(int i=0; i<5; i++)
{
number = random.Next();
if(number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}
Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
return checkCode;
}
private void CreateCheckCodeImage(string checkCode)
{
if(checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for(int i=0; i<25; i++)
{
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);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
//画图片的前景噪音点
for(int i=0; i<100; 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.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}

hyj122 2005-09-13
  • 打赏
  • 举报
回复
谢谢1!!!
JzeroBiao 2005-09-13
  • 打赏
  • 举报
回复
asp.net如何实现验证码
-----------------------------------------------------
Code.aspx.cs

private void Page_Load(object sender, System.EventArgs e)
{
string sCode= MyClass.Function.GetRnd(4);
Session["Code"]=sCode;//--Session变量验证码
Bitmap oBmp = new Bitmap(40,20);
Graphics oG = Graphics.FromImage(oBmp);
oG.Clear(Color.White);
oG.DrawString(sCode,new Font("宋体",12,FontStyle.Bold),new HatchBrush(HatchStyle.LargeConfetti,
Color.FromArgb(200,Color.Blue),Color.FromArgb(200,Color.LightBlue)),0,5);
oBmp.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
}

Login.aspx 前台
<image src="Code.aspx">
---------------------------------------------------
cxx1997 2005-09-13
  • 打赏
  • 举报
回复
www.itsogo.net/2005/8-27/200582795816.html
dev.yesky.com/msdn/103/2085603.shtml

看看就会
hyj122 2005-09-13
  • 打赏
  • 举报
回复
up
Jasonchen82 2005-09-13
  • 打赏
  • 举报
回复
#region 实现一个验证码的类

public class ValidateCode
{
private Bitmap validateimage;
private Graphics g;
public ValidateCode()
{
validateimage = new Bitmap(120, 30, PixelFormat.Format24bppRgb);
g = Graphics.FromImage(validateimage);
}

public void DrawValidateCode(Page e, string i)
{
g.DrawString(i, new Font("黑体",16,FontStyle.Bold),new SolidBrush(Color.White),new PointF(2,4));
g.FillRectangle(new LinearGradientBrush(new Point(0,0), new Point(120,30), Color.FromArgb(0,0,0,0),Color.FromArgb(255,255,255,255)),0,0,120,30);
//validateimage.Save(e.OutputStream, ImageFormat.Jpeg);
g.Save();
MemoryStream ms=new MemoryStream();
validateimage.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
e.Response.ClearContent();
e.Response.ContentType="image/gif";
e.Response.BinaryWrite(ms.ToArray());
e.Response.End();
//validateimage.Save(e.MapPath("NumImage/ValidateImage.gif"), ImageFormat.Gif);
//e.End();
}
}

#endregion


private void MakeValidateCode()
{
char[] s = new char[]{'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 num = "";
Random r = new Random();
for(int i = 0; i < 5; i++)
{
num += s[r.Next(0, s.Length)].ToString();
}
((LoginUserInfo)Session["LoginUserInfo"]).ValidateNum = num;
TextBox3.Text = "";
}


快给分吧!!!

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧