怎么做验证码

aaaa6965921 2010-05-14 05:04:36
谁跟我讲下思路 我就知道第一步随机生成图片 第二步 随机生成数字 第三步 让数字和图片 配合 我就知道这些
...全文
689 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlwork_84 2011-08-01
  • 打赏
  • 举报
回复
wanghuaide 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 ds252743641 的回复:]

[code=C#]//---------------------------------------------

// RandomImage.aspx.cs验证码生成页面后台

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.……
[/Quote]
这个比较完整了
ITtaowmdj 2010-05-14
  • 打赏
  • 举报
回复
学习了
aaaa6965921 2010-05-14
  • 打赏
  • 举报
回复
private void CreateCheckCodeImage(string checkCode) 参数错了吧
itliyi 2010-05-14
  • 打赏
  • 举报
回复
google
aaaa6965921 2010-05-14
  • 打赏
  • 举报
回复
原来 图片是按照字符产生的
xiaobaiso 2010-05-14
  • 打赏
  • 举报
回复
学习快乐
ds252743641 2010-05-14
  • 打赏
  • 举报
回复
[code=C#]//---------------------------------------------

// RandomImage.aspx.cs验证码生成页面后台

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;

public partial class RandomImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

CreateCheckCodeImage(GenerateCheckCode());
}
/// <summary>
/// 生成随机校验码字符串
/// </summary>
/// <returns>生成的随机校验码字符串</returns>
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["CheckCode"] = strCode;
return strCode;
}

/// <summary>
/// 根据校验码输出图片
/// </summary>
/// <param name="checkCode">产生的随机校验码</param>
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();
//输出流的HTTP MIME类型设置为"image/Png"
Response.ContentType = "image/Jpeg";
//输出图片的二进制流
Response.BinaryWrite(ms.ToArray());
}
}
finally
{
//释放Bitmap对象和Graphics对象
g.Dispose();
image.Dispose();
}
}
}
//--------------------------------------------------------------------

//测试页面 dafault.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true" %>

<!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>
<asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl="~/RandomImage.aspx" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /><hr />
<asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
//----------------------------------------------------

//测试页面后台

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
if (string.Compare(Session["CheckCode"].ToString(), this.txtCode.Text, true) != 0)
{
lblMsg.Text = "验证码错误,请输入正确的验证码。";
return;
}
else
{
lblMsg.Text = "成功";
}
}
}
aaaa6965921 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ray_yang 的回复:]
然后把 数字记录在session里
接着把 图片write到前台,
然后在注册的页面里 ,验证码的图片 的 src指定生成图片的页面。
onclick的时候,重新给图片的src赋值
[/Quote]
不明白了 能不能写个代码
aaaa6965921 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mayonglong 的回复:]
C# code

public void MakeCheckCode(string checkCodeCookieName, int len)
{
//这儿定义你期待产生的随机字母
string[] arrLetter = new string[] {
……
[/Quote]怎么让他们匹配
taikonhmao67 2010-05-14
  • 打赏
  • 举报
回复
1楼挺好
skep99 2010-05-14
  • 打赏
  • 举报
回复
gdi,写数字,加正弦扭曲变形,x轴y轴方向坐标分别位移随机数,基本人看着都费劲了
mayonglong 2010-05-14
  • 打赏
  • 举报
回复

public void MakeCheckCode(string checkCodeCookieName, int len)
{
//这儿定义你期待产生的随机字母
string[] arrLetter = new string[] {
"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "2", "3", "4", "5", "6", "7", "8", "9"
};
//随机颜色
Color[] arrColor = new Color[] { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Green, Color.Brown, Color.DarkCyan, Color.Blue };
//随机字体
string[] arrFont = new string[] { "Arial", "Verdana" };

Random romTemp = new Random();
string strCheckCode = "";
//产生随机字符串
for (int i = 1; i <= len; i++)
{
strCheckCode = strCheckCode + arrLetter[romTemp.Next(arrLetter.Length)];
}
strCheckCode = strCheckCode.ToUpper();
//将值写入cook
HttpCookie cookTemp = new HttpCookie(checkCodeCookieName);
cookTemp.Values.Add("CheckCode", strCheckCode);
this.Context.Response.Cookies.Add(cookTemp);
int intWidth = strCheckCode.Length * 13; //计算图片需要的宽度
Bitmap bitmap1 = new Bitmap(intWidth, 0x16);
//绘图
Graphics graphics1 = Graphics.FromImage(bitmap1);
graphics1.Clear(Color.White);
for (int i = 0; i < 5; i++)
{
//随机字母宽度
int intLetterWidth = romTemp.Next(bitmap1.Width);
//随机字母高度
int intLetterHeight = romTemp.Next(bitmap1.Height);
graphics1.DrawRectangle(new Pen(Color.DarkGray, 0f), intLetterWidth, intLetterHeight, 1, 1);
}
for (int i = 0; i < strCheckCode.Length; i++)
{
int num7 = romTemp.Next(arrColor.Length);
int num8 = romTemp.Next(arrFont.Length);
Font font1 = new Font(arrFont[num8], 10f, FontStyle.Bold);
Brush brush1 = new SolidBrush(arrColor[num7]);
graphics1.DrawString(strCheckCode.Substring(i, 1), font1, brush1, (float)(3 + (i * 11)), 3f);
}
graphics1.DrawRectangle(new Pen(Color.LightGray, 0f), 0, 0, bitmap1.Width - 1, bitmap1.Height - 1);
graphics1.Dispose();
System.IO.MemoryStream stream1 = new System.IO.MemoryStream();
bitmap1.Save(stream1, System.Drawing.Imaging.ImageFormat.Gif);
this.Context.Response.ClearContent();
this.Context.Response.ContentType = "image/Gif";
this.Context.Response.BinaryWrite(stream1.ToArray());
bitmap1.Dispose();
graphics1.Dispose();
}
flyerwing 2010-05-14
  • 打赏
  • 举报
回复
能个画布,然后在画布上画些个数字字母的,并且扭曲点,然后显示出来就OK的了。
Ray_Yang 2010-05-14
  • 打赏
  • 举报
回复
然后把 数字记录在session里
接着把 图片write到前台,
然后在注册的页面里 ,验证码的图片 的 src指定生成图片的页面。
onclick的时候,重新给图片的src赋值

110,567

社区成员

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

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

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