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

推荐订阅源

博客园 - 三生石上(FineUI控件)
D
Docker
GbyAI
GbyAI
宝玉的分享
宝玉的分享
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Vercel News
Vercel News
博客园_首页
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
S
SegmentFault 最新的问题
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
V
V2EX
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Visual Studio Blog
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - hdbb30

SharePoint 通讯录查询 - hdbb30 - 博客园 ASP.Net调用Excel组件错误的问题解决 创建强命名程序集 缩小数据库日志文件 SharePoint首页添加天气预报 - hdbb30 - 博客园 工作流产品比较 运用Google Ajax搜索API ASP.NET状态保存方法 ASP.NET保持用户状态的九种选择 ASP.NET中Cookie编程的基础知识 Asp.Net 动态生成验证码 用ASP.NET构建完整E-mail发送系统 弹出确认对话框的自定义Web服务器控件ConfirmButton GRIDVIEW 用法 Net/WEB开发常用的方法、函数 Asp.Net 动态生成验证码 ASP.NET程序中常用的三十三种代码 在DataGrid中利用CheckBox全选所有行 ASP.NET中17种正则表达式
生成中文的验证码(ASP.NET C#)
hdbb30 · 2006-09-22 · via 博客园 - hdbb30

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;

using System.Text;

namespace RandomChineseCode
{
 /// <summary>
 /// RandomChineseCode 的摘要说明。
 /// </summary>
 public class RandomChineseCode : System.Web.UI.Page
 {
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面

   string checkCode = ConvertChinese(4);
   this.Session["CheckCode"] = checkCode;
   CreateImage(checkCode);
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion


  public static object[] CreateRegionCode(int strlength)
  {
   //定义一个字符串数组储存汉字编码的组成元素
   string[] rBase=new String [16]{"0","1","2","3","4","5","6","7","8" ,"9","a","b","c","d","e","f"};
            
   Random rnd=new Random();
        
   //定义一个object数组用来
   object[] bytes=new object[strlength];

   /**//*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中
             每个汉字有四个区位码组成
             区位码第1位和区位码第2位作为字节数组第一个元素
             区位码第3位和区位码第4位作为字节数组第二个元素
            */
   for(int i=0;i<strlength;i++)
   {
    //区位码第1位
    int r1=rnd.Next(11,14);
    string str_r1=rBase[r1].Trim();

    //区位码第2位
    rnd=new Random(r1*unchecked((int)DateTime.Now.Ticks)+i);//更换随机数发生器的 种子避免产生重复值
    int r2;
    if (r1==13)
    {
     r2=rnd.Next(0,7);
    }
    else
    {
     r2=rnd.Next(0,16);
    }
    string str_r2=rBase[r2].Trim();

    //区位码第3位
    rnd=new Random(r2*unchecked((int)DateTime.Now.Ticks)+i);
    int r3=rnd.Next(10,16);
    string str_r3=rBase[r3].Trim();

    //区位码第4位
    rnd=new Random(r3*unchecked((int)DateTime.Now.Ticks)+i);
    int r4;
    if (r3==10)
    {
     r4=rnd.Next(1,16);
    }
    else if (r3==15)
    {
     r4=rnd.Next(0,15);
    }
    else
    {
     r4=rnd.Next(0,16);
    }
    string str_r4=rBase[r4].Trim();

    //定义两个字节变量存储产生的随机汉字区位码
    byte byte1=Convert.ToByte(str_r1 + str_r2,16);
    byte byte2=Convert.ToByte(str_r3 + str_r4,16);
    //将两个字节变量存储在字节数组中
    byte[] str_r=new byte[]{byte1,byte2};

    //将产生的一个汉字的字节数组放入object数组中
    bytes.SetValue(str_r,i);
                
   }

   return bytes;

  }

  private string ConvertChinese(int len)
  {
   //获取GB2312编码页(表)
   Encoding gb=Encoding.GetEncoding("gb2312");

   //调用函数产生4个随机中文汉字编码
   object[] bytes=CreateRegionCode(len);

   //根据汉字编码的字节数组解码出中文汉字
   string str1=gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
   string str2=gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
   string str3=gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
   string str4=gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));

   return str1 + str2 +str3 +str4;
  }


  private void CreateImage(string checkCode)
  {
   int iwidth = (int)(checkCode.Length * 18);
   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.White);
   //g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
   g.Clear(Color.Coral);
   //这两种方法都可以改变生成图片的背景颜色
   
   
   g.DrawString(checkCode, f, b, 3, 3);

//   //下面那个for循环用来生成一些随机的水平线
//   Pen blackPen = new Pen(Color.Tomato, 0);
//   Random rand = new Random();
//   for (int i=0;i<2;i++)
//   {
//    int y = rand.Next(image.Height);
//    g.DrawLine(blackPen,0,y,image.Width,y);//画横线
//   }
   
   
           
   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();
  }
 }
}