0

0

asp.net生成验证码(纯数字)

高洛峰

高洛峰

发布时间:2017-01-13 14:54:29

|

1217人浏览过

|

来源于php中文网

原创

checkcode.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; 
///  
/// CheckCode 的摘要说明 
///  
public class CheckCode 
{ 
public CheckCode() 
{ 
// 
// TODO: 在此处添加构造函数逻辑 
// 
} 
public static void DrawImage() 
{ 
CheckCode img = new CheckCode(); 
HttpContext.Current.Session["CheckCode"] = img.RndNum(4); 
img.checkCodes(HttpContext.Current.Session["CheckCode"].ToString()); 
} 
///  
/// 生成验证图片 
///  
/// 验证字符 
private void checkCodes(string checkCode) 
{ 
int iwidth = (int)(checkCode.Length * 13); 
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 23); 
Graphics g = Graphics.FromImage(image); 
g.Clear(Color.White); 
//定义颜色 
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; 
//定义字体 
string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" }; 
Random rand = new Random(); 
//随机输出噪点 
for (int i = 0; i < 50; i++) 
{ 
int x = rand.Next(image.Width); 
int y = rand.Next(image.Height); 
g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1); 
} 
//输出不同字体和颜色的验证码字符 
for (int i = 0; i < checkCode.Length; i++) 
{ 
int cindex = rand.Next(7); 
int findex = rand.Next(5); 
Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold); 
Brush b = new System.Drawing.SolidBrush(c[cindex]); 
int ii = 4; 
if ((i + 1) % 2 == 0) 
{ 
ii = 2; 
} 
g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 12), ii); 
} 
//画一个边框 
g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1); 
//输出到浏览器 
System.IO.MemoryStream ms = new System.IO.MemoryStream(); 
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
HttpContext.Current.Response.ClearContent(); 
//Response.ClearContent(); 
HttpContext.Current.Response.ContentType = "image/Jpeg"; 
HttpContext.Current.Response.BinaryWrite(ms.ToArray()); 
g.Dispose(); 
image.Dispose(); 
} 
///  
/// 生成随机的字母 
///  
/// 生成字母的个数 
/// string 
private string RndNum(int VcodeNum) 
{ 
string Vchar = "0,1,2,3,4,5,6,7,8,9"; 
string[] VcArray = Vchar.Split(','); 
string VNum = ""; //由于字符串很短,就不用StringBuilder了 
int temp = -1; //记录上次随机数值,尽量避免生产几个一样的随机数 
//采用一个简单的算法以保证生成随机数的不同 
Random rand = new Random(); 
for (int i = 1; i < VcodeNum + 1; i++) 
{ 
if (temp != -1) 
{ 
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks)); 
} 
int t = rand.Next(VcArray.Length); 
if (temp != -1 && temp == t) 
{ 
return RndNum(VcodeNum); 
} 
temp = t; 
VNum += VcArray[t]; 
} 
return VNum; 
} 
}

再建立一个引用类的页面checkCode.aspx前台不用写东西,后台引用我们创建的类的DrawImage()方法即可。 

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; 
public partial class checkCode : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
CheckCode.DrawImage(); 
} 
}

下面我们在需要验证码的页面引用checkCode.aspx页面即可。 
前台 

 
看不清,请点击我! 

后台判断 

怪兽AI数字人
怪兽AI数字人

数字人短视频创作,数字人直播,实时驱动数字人

下载
protected void imgBtnLogin_Click(object sender, ImageClickEventArgs e) 
{ 
if(this.Validator.Text==Session["CheckCode"].ToString()) 
{ 
//。。。。 
} 
else 
{ 
Response.Write(""); 
return; 
} 
}

以上代码请根据实际情况作适当修改。

更多asp.net生成验证码(纯数字)相关文章请关注PHP中文网!

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
go语言 注释编码
go语言 注释编码

本专题整合了go语言注释、注释规范等等内容,阅读专题下面的文章了解更多详细内容。

2

2026.01.31

go语言 math包
go语言 math包

本专题整合了go语言math包相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

go语言输入函数
go语言输入函数

本专题整合了go语言输入相关教程内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

golang 循环遍历
golang 循环遍历

本专题整合了golang循环遍历相关教程,阅读专题下面的文章了解更多详细内容。

0

2026.01.31

Golang人工智能合集
Golang人工智能合集

本专题整合了Golang人工智能相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

2026赚钱平台入口大全
2026赚钱平台入口大全

2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

76

2026.01.31

高干文在线阅读网站大全
高干文在线阅读网站大全

汇集热门1v1高干文免费阅读资源,涵盖都市言情、京味大院、军旅高干等经典题材,情节紧凑、人物鲜明。阅读专题下面的文章了解更多详细内容。

73

2026.01.31

无需付费的漫画app大全
无需付费的漫画app大全

想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。

67

2026.01.31

漫画免费在线观看地址大全
漫画免费在线观看地址大全

想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。

19

2026.01.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 8.2万人学习

Git 教程
Git 教程

共21课时 | 3.2万人学习

Laravel---API接口
Laravel---API接口

共7课时 | 0.6万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号