我在login.aspx中写了<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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">
<div>
<input type="text" name="LoginName" /><br />
<input type="password" name="LoginPwd" />
<asp:Image ID="imgValid" runat="server" /><br />
<input type="submit" value="登录" />
</div>
</form>
</body>
</html>
代码。
在login.aspx.cs中写了
using System;
using System.Data;
using System.Configuration;
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;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string num = this.getRandomValidate(4);
getImageValidate(num);
//this.imgValid.ImageUrl = img;
}
public string getRandomValidate(int len)
{
int num;
int tem;
string rtuStr = "";
Random ran = new Random();
for (int i = 0; i < len; i++)
{
num = ran.Next();
/*
* 这里可以选择生成字符和数字组合的验证码
*/
tem = num % 10 + '0';//生成数字
//tem = num % 26 + 'A';//生成字符
rtuStr += Convert.ToChar(tem).ToString();
}
return rtuStr;
}
private void getImageValidate(string strValue)
{
//string str = "OO00"; //前两个为字母O,后两个为数字0
int width = Convert.ToInt32(strValue.Length * 30); //计算图像宽度
Bitmap img = new Bitmap(width, 50);
Graphics gfc = Graphics.FromImage(img); //产生Graphics对象,进行画图
gfc.Clear(Color.White);
//gfc.DrawLine(img);
//写验证码,需要定义Font
Font font = new Font("隶书", 26, FontStyle.Bold);
System.Drawing.Drawing2D.LinearGradientBrush brush =
new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.DarkOrchid, Color.Blue, 3.5f, true);
gfc.DrawString(strValue, font, brush, 3, 2);
//drawPoint(img);
gfc.DrawRectangle(new Pen(Color.DarkBlue), 0, 0, img.Width - 1, img.Height - 1);
this.drawPoint(img);
this.drawLine(gfc, img);
//将图像添加到页面
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
//return img;
//更改Http头
Response.ClearContent();
Response.ContentType = "image/gif";
Response.BinaryWrite(ms.ToArray());
//Dispose
gfc.Dispose();
img.Dispose();
Response.End();
}
private void drawPoint(Bitmap img)
{
/*
//选择画100个点,可以根据实际情况改变
for (int i = 0; i < 100; i++)
{
int x = ran.Next(img.Width);
int y = ran.Next(img.Height);
img.SetPixel(x,y,Color.FromArgb(ran.Next()));//杂点颜色随机
}*/
Random ran = new Random();
int col = ran.Next();//在一次的图片中杂店颜色相同
for (int i = 0; i < 100; i++)
{
int x = ran.Next(img.Width);
int y = ran.Next(img.Height);
img.SetPixel(x, y, Color.FromArgb(col));
}
}
private void drawLine(Graphics gfc, Bitmap img)
{
Random ran = new Random();
//选择画10条线,也可以增加,也可以不要线,只要随机杂点即可
for (int i = 0; i < 10; i++)
{
int x1 = ran.Next(img.Width);
int y1 = ran.Next(img.Height);
int x2 = ran.Next(img.Width);
int y2 = ran.Next(img.Height);
gfc.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); //注意画笔一定要浅颜色,否则验证码看不清楚
}
}
}
结果在页面上只显示验证码,不显示表单,我是个初学者,不知道怎么回事,为什么光把验证码显示出来了,我在login.aspx中写的表单为什么不显示呢?大虾来指点下。谢谢
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1">
<div>
<input type="text" name="LoginName" /><br />
<input type="password" name="LoginPwd" />
<asp:Image ID="imgValid" runat="server" /><br />
<input type="submit" value="登录" />
</div>
</form>
</body>
</html>
代码。
在login.aspx.cs中写了
using System;
using System.Data;
using System.Configuration;
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;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string num = this.getRandomValidate(4);
getImageValidate(num);
//this.imgValid.ImageUrl = img;
}
public string getRandomValidate(int len)
{
int num;
int tem;
string rtuStr = "";
Random ran = new Random();
for (int i = 0; i < len; i++)
{
num = ran.Next();
/*
* 这里可以选择生成字符和数字组合的验证码
*/
tem = num % 10 + '0';//生成数字
//tem = num % 26 + 'A';//生成字符
rtuStr += Convert.ToChar(tem).ToString();
}
return rtuStr;
}
private void getImageValidate(string strValue)
{
//string str = "OO00"; //前两个为字母O,后两个为数字0
int width = Convert.ToInt32(strValue.Length * 30); //计算图像宽度
Bitmap img = new Bitmap(width, 50);
Graphics gfc = Graphics.FromImage(img); //产生Graphics对象,进行画图
gfc.Clear(Color.White);
//gfc.DrawLine(img);
//写验证码,需要定义Font
Font font = new Font("隶书", 26, FontStyle.Bold);
System.Drawing.Drawing2D.LinearGradientBrush brush =
new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.DarkOrchid, Color.Blue, 3.5f, true);
gfc.DrawString(strValue, font, brush, 3, 2);
//drawPoint(img);
gfc.DrawRectangle(new Pen(Color.DarkBlue), 0, 0, img.Width - 1, img.Height - 1);
this.drawPoint(img);
this.drawLine(gfc, img);
//将图像添加到页面
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
//return img;
//更改Http头
Response.ClearContent();
Response.ContentType = "image/gif";
Response.BinaryWrite(ms.ToArray());
//Dispose
gfc.Dispose();
img.Dispose();
Response.End();
}
private void drawPoint(Bitmap img)
{
/*
//选择画100个点,可以根据实际情况改变
for (int i = 0; i < 100; i++)
{
int x = ran.Next(img.Width);
int y = ran.Next(img.Height);
img.SetPixel(x,y,Color.FromArgb(ran.Next()));//杂点颜色随机
}*/
Random ran = new Random();
int col = ran.Next();//在一次的图片中杂店颜色相同
for (int i = 0; i < 100; i++)
{
int x = ran.Next(img.Width);
int y = ran.Next(img.Height);
img.SetPixel(x, y, Color.FromArgb(col));
}
}
private void drawLine(Graphics gfc, Bitmap img)
{
Random ran = new Random();
//选择画10条线,也可以增加,也可以不要线,只要随机杂点即可
for (int i = 0; i < 10; i++)
{
int x1 = ran.Next(img.Width);
int y1 = ran.Next(img.Height);
int x2 = ran.Next(img.Width);
int y2 = ran.Next(img.Height);
gfc.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); //注意画笔一定要浅颜色,否则验证码看不清楚
}
}
}
结果在页面上只显示验证码,不显示表单,我是个初学者,不知道怎么回事,为什么光把验证码显示出来了,我在login.aspx中写的表单为什么不显示呢?大虾来指点下。谢谢
ValidCode.aspx的后台就用你这里的