为什么点按扭的时候,页面显示的是:"输入有误"
请大家帮忙看一下哪里出错了//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;/// <summary>
/// checkCode 的摘要说明
/// </summary>
public class checkCode
{
public checkCode()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 生成验证码序列
/// </summary>
/// <returns>返回验证码序列</returns>
public String CreateCheckCode()
{
//验证码存放数组
char[] CoedArray ={ '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' };
//接收验证码
String checkCode = "";
Random rd = new Random();
//生面四个字符的验证码
for (int i = 0; i < 4; i++)
{
checkCode += CoedArray[rd.Next(CoedArray.Length)];
}
//返回验证码
return checkCode;
}
/// <summary>
/// 创建验证码图片
/// </summary>
/// <param name="FilePath">存放验证码图片的路径</param>
public void CreateCheckCodeIMG(String FilePath)
{
//生成验证码的高和宽
int iWidth = 55;
int iHeight = 22;
//验证码的字体/大小/样式
Font font = new Font("Arial", 12, FontStyle.Bold);
//定义一支写黑色字的刷笔
SolidBrush brush = new SolidBrush(Color.Black);
//定义两支绘制干扰线的笔
Pen pen1 = new Pen(Color.Gray, 0);
Pen pen2 = new Pen(Color.FromArgb(255, 100, 100, 100), 0);
//创建位图
Bitmap img = new Bitmap(iWidth, iHeight);
Graphics g = Graphics.FromImage(img);
g.Clear(ColorTranslator.FromHtml("#f0f0f0"));
Rectangle rect = new Rectangle(5, 2, iWidth, iHeight);
Random rd = new Random();
//绘制两根横向的干扰线
for (int i = 0; i < 2; i++)
{
Point p1=new Point(0,rd.Next(iHeight));
Point p2 = new Point(iWidth, rd.Next(iHeight));
g.DrawLine(pen1, p1, p2);
}
//绘制四根纵向的干扰线
for (int i = 0; i < 4; i++)
{
Point p1 = new Point(rd.Next(iWidth), 0);
Point p2 = new Point(rd.Next(iWidth), iHeight);
g.DrawLine(pen2,p1,p2);
}
//生成验证码
g.DrawString(CreateCheckCode(), font, brush, rect);
//保存验证码
img.Save(FilePath, ImageFormat.Jpeg);
//释放资源
g.Dispose();
img.Dispose();
}
}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public partial class _Default : System.Web.UI.Page
{
string Code = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
img();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
String checkCode = TextBox1.Text.ToString().ToLower();//将输入的字符转为小写
Response.Write(Code);//
if (checkCode == Code)
{
Response.Write("输入正确");
}
else
{
Response.Write("输入有误");
}
img();
}
protected void img()
{
String FilePath = Server.MapPath("img") + "\\" + "checkCode.jpg";
checkCode cc = new checkCode();
cc.CreateCheckCodeIMG("FilePath");
Image1.ImageUrl = "img\\checkCode.jpg";
Code = cc.CreateCheckCode();
Response.Write(Code);
}
}
请大家帮忙看一下哪里出错了//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;/// <summary>
/// checkCode 的摘要说明
/// </summary>
public class checkCode
{
public checkCode()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 生成验证码序列
/// </summary>
/// <returns>返回验证码序列</returns>
public String CreateCheckCode()
{
//验证码存放数组
char[] CoedArray ={ '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' };
//接收验证码
String checkCode = "";
Random rd = new Random();
//生面四个字符的验证码
for (int i = 0; i < 4; i++)
{
checkCode += CoedArray[rd.Next(CoedArray.Length)];
}
//返回验证码
return checkCode;
}
/// <summary>
/// 创建验证码图片
/// </summary>
/// <param name="FilePath">存放验证码图片的路径</param>
public void CreateCheckCodeIMG(String FilePath)
{
//生成验证码的高和宽
int iWidth = 55;
int iHeight = 22;
//验证码的字体/大小/样式
Font font = new Font("Arial", 12, FontStyle.Bold);
//定义一支写黑色字的刷笔
SolidBrush brush = new SolidBrush(Color.Black);
//定义两支绘制干扰线的笔
Pen pen1 = new Pen(Color.Gray, 0);
Pen pen2 = new Pen(Color.FromArgb(255, 100, 100, 100), 0);
//创建位图
Bitmap img = new Bitmap(iWidth, iHeight);
Graphics g = Graphics.FromImage(img);
g.Clear(ColorTranslator.FromHtml("#f0f0f0"));
Rectangle rect = new Rectangle(5, 2, iWidth, iHeight);
Random rd = new Random();
//绘制两根横向的干扰线
for (int i = 0; i < 2; i++)
{
Point p1=new Point(0,rd.Next(iHeight));
Point p2 = new Point(iWidth, rd.Next(iHeight));
g.DrawLine(pen1, p1, p2);
}
//绘制四根纵向的干扰线
for (int i = 0; i < 4; i++)
{
Point p1 = new Point(rd.Next(iWidth), 0);
Point p2 = new Point(rd.Next(iWidth), iHeight);
g.DrawLine(pen2,p1,p2);
}
//生成验证码
g.DrawString(CreateCheckCode(), font, brush, rect);
//保存验证码
img.Save(FilePath, ImageFormat.Jpeg);
//释放资源
g.Dispose();
img.Dispose();
}
}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public partial class _Default : System.Web.UI.Page
{
string Code = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
img();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
String checkCode = TextBox1.Text.ToString().ToLower();//将输入的字符转为小写
Response.Write(Code);//
if (checkCode == Code)
{
Response.Write("输入正确");
}
else
{
Response.Write("输入有误");
}
img();
}
protected void img()
{
String FilePath = Server.MapPath("img") + "\\" + "checkCode.jpg";
checkCode cc = new checkCode();
cc.CreateCheckCodeIMG("FilePath");
Image1.ImageUrl = "img\\checkCode.jpg";
Code = cc.CreateCheckCode();
Response.Write(Code);
}
}
{
String FilePath = Server.MapPath("img") + "\\" + "checkCode.jpg";
checkCode cc = new checkCode();
cc.CreateCheckCodeIMG("FilePath");
Image1.ImageUrl = "img\\checkCode.jpg";
Code = cc.CreateCheckCode(); //你得到的 Code是新生成的,和生成图片的不是一个
Response.Write(Code);
} 这句:g.DrawString(CreateCheckCode(), font, brush, rect);
http://www.cnblogs.com/gwazy/archive/2005/04/18/139510.html