---
title: "验证码的三种做法[纯数字][纯汉字][字母\\数字\\汉字混合]C# "
author: "water84222"
date: "2006-07-11T09:51:00Z"
tags: ["C", "C#", "C++", "VB", "VB.NET"]
slug: "c"
draft: false
lastmod: "2006-07-11T09:51:00Z"
id: 156616
url: "https://onlyLove.iteye.com/blog/156616"
---

网上有很多关于验证码做法的资料,但是真正做得完整的却非常少,所以昨天晚上参考了作者：CSDN 上kwklover 的[在ASP.NET下实现数字和字符相混合的验证码](http://whxylm.cnblogs.com/smartcn/archive/2004/07/07/22091.html) (混合,完整,遗憾的是vb.net,说到这里我想我得感谢stone(QQ9337874),因为我在把VB译成C#的过程中几番出错,最终这段翻译由他完成)!!

随机汉字参考了[http://navicy.cnblogs.com/archive/2005/05/08/150756.html](http://navicy.cnblogs.com/archive/2005/05/08/150756.html)这篇文章的,

而最终的纯数字以及GDI+绘图则是从□☆近三十☆(QQ19302038)传给我的一个OA案例里面分离出来的!

所以完整的说,这里面几乎都是别人做好了的,我只是整理出来了!

完整的下载地址是　[http://www.cnblogs.com/Files/thcjp/gdi.rar](http://thcjp.cnblogs.com/Files/thcjp/gdi.rar)　

下面把代码的重点部分贴出来，如果看源文件有什么不明白可以问偶　ＱＱ１１０５３５８０８

效果如图(我确实懒了点,按纽的名字都没有改,哈哈)  
需要说明的是,因为提交对比后,页面是重新刷新了,所以文本框里面看见的和后面图片显示不一样,这个不需要太多说明吧!!

 混合模式刷新出现约界错误 已经被 stone(QQ9337874) 修正!!

![](http://thcjp.cnblogs.com/images/cnblogs_com/thcjp/aaaa.jpg)

![](http://thcjp.cnblogs.com/images/cnblogs_com/thcjp/bbb.jpg)

![](http://thcjp.cnblogs.com/images/cnblogs_com/thcjp/ccc.JPG)

![](http://thcjp.cnblogs.com/images/cnblogs_com/thcjp/ddd.JPG)

下面是ＧＤＩ＋绘图的代码段，以及显示的调用等

![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)using System;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)using System.Data;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)using System.Configuration;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)using System.Collections;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)using System.Web;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)using System.Web.Security;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)using System.Web.UI;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)using System.Web.UI.WebControls;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)using System.Web.UI.WebControls.WebParts;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)using System.Web.UI.HtmlControls;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)using System.Drawing.Drawing2D;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)using System.Drawing.Imaging;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)using System.Drawing.Text;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)using System.Drawing;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)using System.Text;   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)public partial class png : System.Web.UI.Page  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)![](http://thcjp.cnblogs.com/Images/dot.gif){  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)    private readonly string ImagePath = "Validator.jpg";  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)    private static string gif = "";  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)    protected void Page\_Load(object sender, EventArgs e)  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)    ![](http://thcjp.cnblogs.com/Images/dot.gif){  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        switch (Request.QueryString\["aa"])  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        ![](http://thcjp.cnblogs.com/Images/dot.gif){  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            case "1":  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                gif = stxt();  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                Session\["gif"]= stxt();  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                break;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            case "2":  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                gif = GetRandomint();  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                 Session\["gif"]= GetRandomint();  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                break;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            case "3":  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                gif = RndNum(3);  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                 Session\["gif"]= RndNum(3);  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                break;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            default:  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                gif = RndNum(3);  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                 Session\["gif"]= GetRandomint();  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                break;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)          
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        /\*\*////创建Bmp位图  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        Bitmap bitMapImage = new System.Drawing.Bitmap(Server.MapPath(ImagePath));  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        Graphics graphicImage = Graphics.FromImage(bitMapImage);  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        /\*\*////设置画笔的输出模式  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        graphicImage.SmoothingMode = SmoothingMode.HighSpeed;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        /\*\*////添加文本字符串  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        graphicImage.DrawString(gif, new Font("Arial", 20, FontStyle.Bold), SystemBrushes.WindowText, new Point(0, 0));  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        /\*\*////设置图像输出的格式  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        Response.ContentType = "image/jpeg";  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        /\*\*////保存数据流  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        bitMapImage.Save(Response.OutputStream, ImageFormat.Jpeg);  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        /\*\*////释放占用的资源  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        graphicImage.Dispose();  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        bitMapImage.Dispose();  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)    }

返回纯数字

![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)    private String GetRandomint()  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)    ![](http://thcjp.cnblogs.com/Images/dot.gif){  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        Random random = new Random();  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        return (random.Next(100000, 999999).ToString());  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)    }

返回文字＼数字＼字母混合的

![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)public static String RndNum(int VcodeNum)  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)    ![](http://thcjp.cnblogs.com/Images/dot.gif){  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        String Vchar = "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,W,X,Y,Z,呵,哈,弹,簧,秤,嬉,戏";  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        String\[] VcArray = Vchar.Split(',');  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        String VNum = "";  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        Random random = new Random();  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        for (int i = 1; i &lt;= VcodeNum; i++)  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        ![](http://thcjp.cnblogs.com/Images/dot.gif){  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            int iNum = 0;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            while ((iNum = Convert.ToInt32(VcArray.Length * random.NextDouble())) ==VcArray.Length )  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)            ![](http://thcjp.cnblogs.com/Images/dot.gif){  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                iNum = Convert.ToInt32(VcArray.Length * random.NextDouble());  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            VNum += VcArray\[iNum];  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            // VNum += VcArray\[Convert.ToInt32(VcArray.Length\*random.NextDouble())];  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        return VNum;  
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)    } 

返回汉字的

![](http://thcjp.cnblogs.com/Images/OutliningIndicators/None.gif)       public static object\[] CreateRegionCode(int strlength)   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)        ![](http://thcjp.cnblogs.com/Images/dot.gif){   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            //定义一个字符串数组储存汉字编码的组成元素   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)            string\[] rBase=new String \[16]![](http://thcjp.cnblogs.com/Images/dot.gif){"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)               
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            Random rnd=new Random();   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)           
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            //定义一个object数组用来   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            object\[] bytes=new object\[strlength];   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)            /\*\*//\*\*//\*\*//\*每循环一次产生一个含两个元素的十六进制字节数组，并将其放入bject数组中   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)             每个汉字有四个区位码组成   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)             区位码第1位和区位码第2位作为字节数组第一个元素   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)             区位码第3位和区位码第4位作为字节数组第二个元素   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)            \*/   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            for(int i=0;i&lt;strlength;i++)   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)            ![](http://thcjp.cnblogs.com/Images/dot.gif){   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                //区位码第1位   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                int r1=rnd.Next(11,14);   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                string str\_r1=rBase\[r1].Trim();   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                //区位码第2位   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                rnd=new Random(r1\*unchecked((int)DateTime.Now.Ticks)+i);//更换随机数发生器的种子避免产生重复值   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                int r2;   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                if (r1==13)   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)                ![](http://thcjp.cnblogs.com/Images/dot.gif){   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                    r2=rnd.Next(0,7);   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)                }   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                else   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)                ![](http://thcjp.cnblogs.com/Images/dot.gif){   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                    r2=rnd.Next(0,16);   
![](http://thcjp.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)                }   
![](http://thcjp.cnblogs.co)