中文验证码机制

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;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!Page.IsPostBack)
        
{
            CreateCheckCodeImage(GenerateCheckCode());
        }

    }


    
private string GenerateCheckCode()//返回中文汉字[从汉字编码中解码出来]
    {
        
string checkCode = String.Empty;
        System.Random random 
= new Random();
        Encoding gb 
= Encoding.GetEncoding("gb2312");//中文简体编码
        object[] bytes = CreateRegionCode(5);//调用函数产生5个随机中文汉字编码 
        
//根据汉字编码的字节数组解码出中文汉字 
        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[])));
        
string str5 = gb.GetString((byte[])Convert.ChangeType(bytes[4], typeof(byte[])));
        checkCode 
= str1 + str2 + str3 + str4 + str5;
        
return checkCode;
    }


    
产生波形滤镜效果

    
private 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 * 27.0)), 25);//实例化图片大小
        Graphics g = Graphics.FromImage(image);//创建新图
        try
        
{
            Random random 
= new Random();//生成随机生成器 
            g.Clear(Color.White);//清空图片背景色
            
//画图片的背景噪音线[可以设置数量] 
            for (int i = 0; i < 50; 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("楷体_GB2312"16, (System.Drawing.FontStyle.Bold));//字体设置
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(00, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2ftrue);
            g.DrawString(checkCode, font, brush, 
32);//绘图
            
//画图片的前景噪音点 
            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), 00, 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();
        }

    }

}


posted @ 2008-01-31 09:58  NetStudy  阅读(165)  评论(0)    收藏  举报