asp.net验证码

zhanghang19890621 2010-01-06 08:19:54
protected void Page_Load(object sender, EventArgs e)
{
Response.BufferOutput = true;
Bitmap img = new Bitmap(100, 100, PixelFormat.Format24bppRgb);
Graphics grh = Graphics.FromImage(img);
grh.Clear(ColorTranslator.FromHtml("#336699"));
grh.FillRectangle(new SolidBrush(Color.Yellow), 200, 200, 200, 200);
grh.DrawString(Show(), new Font("宋体", 20), new SolidBrush(Color.Red), new PointF(50, 50));
//Response.ContentType = "image/Jpeg";
img.Save(Response.OutputStream, ImageFormat.Jpeg);
Response.ContentType = "image/Jpeg";
grh.Dispose();
img.Dispose();
Response.End();

}

protected string Show()
{
string str = "";
Random ran = new Random();
for (int i = 0; i < 4; i++)
{
str += ran.Next(1,10);
}
return str;
}

以上是我做的一个验证码测试

可是页面上面的控件 都不显示了 怎么解决

还有这个验证码一出来就上最上面 怎样把这个验证码定位呢 点击看不清换一张 验证码也会变动
...全文
327 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanhua1987 2010-04-01
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zhanghang19890621 的回复:]

问题解决 谢谢大家了 收益匪浅啊

原来img 还能这样用
[/Quote]

把你的代码发给我下呢? yanhua@veeyaa.cn 谢谢
xray2005 2010-01-06
  • 打赏
  • 举报
回复
需要使用验证码页面的前台HTML代码:
<img src="Code.aspx" alt="点击刷新验证码" style="border: none 0px #E0E0E0; height: 30px;
cursor: pointer; width: 80px;" onclick="javascript:location.reload();" />

Code.aspx的后台代码:
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;
public partial class Code : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.CreateValidationCode();
}
private void CreateValidationCode()
{
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();

}

Session["code"] = checkCode;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 15.0)), 30);
Graphics g = Graphics.FromImage(image);

try
{
//生成随机生成器


//清空图片背景色
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.FromArgb(119, 176, 69)), 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.FromArgb(3, 24, 49), Color.FromArgb(3, 101, 45), 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.FromArgb(3, 101, 45)), 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();
}


}
}
悔说话的哑巴 2010-01-06
  • 打赏
  • 举报
回复
帮顶
zhanghang19890621 2010-01-06
  • 打赏
  • 举报
回复
问题解决 谢谢大家了 收益匪浅啊

原来img 还能这样用
Lovely_baby 2010-01-06
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zhanghang19890621 的回复:]
引用 3 楼 lovely_baby 的回复:
image控件  绑定你得到的值 ??


不太明白你的意思

[/Quote]
类似于4楼的方法
zhanghang19890621 2010-01-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lovely_baby 的回复:]
image控件  绑定你得到的值 ??
[/Quote]

不太明白你的意思
zhanghang19890621 2010-01-06
  • 打赏
  • 举报
回复
1楼你的代码 好像是在网上找到哦 我在网上搜索资料看到过这段代码 里面有很多我不明白的意思
小_虎 2010-01-06
  • 打赏
  • 举报
回复
ps:貌似你现在加不了分
newdigitime 2010-01-06
  • 打赏
  • 举报
回复
把你上面的代码保存为gimg.aspx

在需要用到验证码的页面里相应位置设置一个<img>,将其src设置为gimg.aspx

如<img src=gimg.aspx>

这样就可以定位了.

实现点击看不清换一张,可以用javascript控制(当然也可以把<img>弄成服务器端控件通过后台代码控制)
如<Img onclick="chimg()" src=gimg.aspx id="ckimg">

function chimg()
{
var num;
num=生成一个随机数
document.getElementById("ckimg").src="gimg.aspx?xx="+num;
}

小_虎 2010-01-06
  • 打赏
  • 举报
回复
<img id="Img1" alt="看不清,请点击我!" onclick="this.src=this.src+'?'" src="CheckCode.aspx"
style="width: 73px; height: 22px" align="left" />

CheckCode.aspx就是你产生验证码,response.BinaryWrite的页面
Lovely_baby 2010-01-06
  • 打赏
  • 举报
回复
image控件 绑定你得到的值 ??
zhanghang19890621 2010-01-06
  • 打赏
  • 举报
回复
没人知道嘛

闲分少了 可以在加哦
wuyq11 2010-01-06
  • 打赏
  • 举报
回复
<asp:label id="Lbl" runat="server" Width="38px">
<IMG src="CheckCode.aspx" align="absmiddle"></asp:label>
protected void Page_Load(object sender, EventArgs e)
{
string strL=new ValidCode().MakeValidateCode();
Session["LZM"] = strL;
new ValidCode().CreateCheckCodeImage(strL);
}

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();
}
return checkCode;
}
public 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);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Gif";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
public void ValidateCode(string VNum)
{
Bitmap validateimage;
Graphics g;
validateimage = new Bitmap(50, 20, PixelFormat.Format24bppRgb);
g = Graphics.FromImage(validateimage);

g.DrawString(VNum, new Font("Comic Sans MS", 12), new SolidBrush(Color.White), new PointF(3, 0));
g.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(110, 20), Color.FromArgb(0, 0, 0, 0), Color.FromArgb(255, 255, 60, 40)), 0, 0, 120, 30);
g.Save();
MemoryStream ms = new MemoryStream();
validateimage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/gif";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
HttpContext.Current.Response.End();
}
/// 生成随机数
public string MakeValidateCode()
{
char[] s = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
string num = "";
Random r = new Random();
for (int i = 0; i < 4; i++)
{
num += s[r.Next(0, s.Length)].ToString();
}
return num;
}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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