惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 雪无止境

WEB 架构布置 YouTube的系统架构 [原创]如何得到网站的最后更新日期 首例全国性电子商务规范将出台 Ajax 及 JavaScript 在各浏览器下的兼容性问题 安装Microsoft VS2003“安装程序检测到另一个程序要求计算机重新启动 ASP.Net 注册组件 如何用正确的方法来写出质量好的软件的75条体会 [原创]让C#驱动机器,为我们奏响电子音乐 [原创]在ASP.NET中为图形加水印的方法 (C#版本) IE 与 FireFox 的 showModalDialog 用户 'sa' 登录失败。原因: 未与信任 SQL Server 连接相关联 常用SQL ASP获取即时ID值(自动编号) ASP ADOX 使用范例 [原创]重载、继承 [原创]ASP开发工具论 [原创]用Data Shape 来取关系数据库的多表记录 [原创]基于角色的Roles
ASP.NET 生成图形验证码 (C#版本)
雪无止境 · 2006-10-02 · via 博客园 - 雪无止境

命名空间:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

        private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            string checkCode = CreateRandomCode(4);
            Session[
"CheckCode"= checkCode;
            CreateImage(checkCode);
        }

        
private string CreateRandomCode(int codeCount)
        
{

            
// 函数功能:产生数字和字符混合的随机字符串
            string allChar = "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,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[] allCharArray = allChar.Split(',');
            
string randomCode = "";
            
int temp = -1;

            Random rand 
= new Random();
            
for(int i = 0; i < codeCount; i++)
            
{
                
if(temp != -1)
                
{
                    rand 
= new Random(i*temp*((int)DateTime.Now.Ticks));
                }

                
int t = rand.Next(35);
                
if(temp == t)
                
{
                    
return CreateRandomCode(codeCount);
                }

                temp 
= t;
                randomCode 
+= allCharArray[t];
            }

            
return randomCode;
        }



        
private void CreateImage(string checkCode)
        
{

            
// 生成图象验证码函数
            int iwidth = (int)(checkCode.Length * 11.5);
            System.Drawing.Bitmap image 
= new System.Drawing.Bitmap(iwidth, 20);
            Graphics g 
= Graphics.FromImage(image);
            Font f 
= new System.Drawing.Font("Arial"10, System.Drawing.FontStyle.Bold);
            Brush b 
= new System.Drawing.SolidBrush(Color.Azure);//字母白色
            
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
            g.Clear(Color.Gray);//背景灰色
            g.DrawString(checkCode, f, b, 33);

            Pen blackPen 
= new Pen(Color.Black, 0);
            Random rand 
= new Random();


            System.IO.MemoryStream ms 
= new System.IO.MemoryStream();
            image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
            Response.ClearContent();
            Response.ContentType 
= "image/Jpeg";
            Response.BinaryWrite(ms.ToArray());
            g.Dispose();
            image.Dispose();

        }