ChaosSea验证码0.20版

来源:互联网 发布:机构运动简图 软件 编辑:程序博客网 时间:2024/05/17 18:49

ChaosSea验证码0.20版

功能说明:
1. 可自定义字体
2. 可自定义最大字号
3. 可自定义随机旋转的角度
4. 可自定义前景随机噪色量
5. 可外部生成随机码,由VerifyCode类处理成图片
6. 可自定义数字与英文字母随机出现的比率
7. 可自定义验证码长度
8. 可自定义验证码背景色
9. 可自定义验证码色彩(噪点色同验证码色一致)
10. 可自定义字体

------------------------------
本人觉得用上随机角度就行了,不用前景噪点,因为这个东东会耗一定的资源

 

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;
using System.Drawing;


namespace CS.Common.Utility
{
    
/// <summary>
    
/// Chaos验证码
    
/// FileName            :    VerifyCode.cs   
    
/// Verion             :       0.20
    
/// Author             :       zhouyu  http://max.cszi.com
    
/// Update            :       2007-10-10
    
/// Description      :       验证码随机旋转一定角度,可使用前景色,背景色效果不大就不用了
    
/// </summary>

    public class VerifyCode
    
{

        
private int _length = 4;            //验证码长度
        private int _fontSize = 18;       //字体最大尺寸
        private int _border = 0;           //边框,0时没有连框
        private Color _backgroundColor = Color.AliceBlue;   //背景色
        private Color _fontColor = Color.Blue;     //验证码色
        private int _rateNumber = 10;       //验证码中的数字出现机率 ,越大出现的数字机率越大
        private string _randomChars;       //随机生成的验证码
        private int _randomAngle = 40;       //随机码的旋转角度
        private string _fontFamily = "Verdana";    //字体
        private int _chaosNumber = 0;        //噪点数量 ,0 时不用

        
private Random random = new Random();  //随机种子,公用

        
public VerifyCode()
        
{
           
        }


        
/// <summary>
        
/// 重载一 :噪点
        
/// </summary>
        
/// <param name="chaosNumber"></param>

        public VerifyCode(int chaosNumber)
        
{
            _chaosNumber 
= chaosNumber;
        }


        
/// <summary>
        
/// 重载二:长度,噪点
        
/// </summary>
        
/// <param name="length"></param>
        
/// <param name="chaosNumber"></param>

        public VerifyCode(int length, int chaosNumber)
        
{
            _length 
= length;
            _chaosNumber 
= chaosNumber;
        }


        
/// <summary>
        
/// 重载三:长度,噪点,数字机率
        
/// </summary>
        
/// <param name="length"></param>
        
/// <param name="chaosNumber"></param>
        
/// <param name="rate">越大,生成的随机码中数字占的比例越多</param>

        public VerifyCode(int length, int chaosNumber, int rate)
        
{
            _length 
= length;
            _chaosNumber 
= chaosNumber;
            _rateNumber 
= rate;
        }



        
.Length 验证码长度(默认4个) 

        
.FontSize 字体最大尺寸(默认18) 

        
.Border 边框(默认0 没有连框)

        
.BackgroundColor 自定义背景色(默认Color.AliceBlue)

        
.FontColor 验证码色(默认Color.Blue)

        
.RandomCode  随机生成的验证码

        
.RateNumber 验证码中的数字出现机率,越大出现的数字机率越大(默认10)

        
.RandomAngle 随机码的旋转角度(默认40度)

        
.FontFamily 字体

        
.ChaosNumber 噪点数量(默认值为2)





        
/// <summary>
        
/// 生成随机验证码
        
/// </summary>

        private void CreateCode()
        
{
            
//有外部输入验证码时不用产生,否则随机生成
            if (!string.IsNullOrEmpty(_randomChars))
            
return; }

            
char code;
            
for (int i = 0; i < _length; i++)
            
{
                
int rand = random.Next();
                
if (rand % _rateNumber == 0)
                
{ code = (char)('A' + (char)(rand % 26)); }
                
else
                
{ code = (char)('0' + (char)(rand % 10)); }
                _randomChars 
+= code.ToString();
            }

        }




        
/// <summary>
        
/// 背景噪点生成
        
/// </summary>
        
/// <param name="graph"></param>

        private void  CreateBackgroundChaos(Bitmap map,Graphics graph)
        
{
            Pen blackPen 
= new Pen(Color.Azure, 0);
            
for (int i = 0; i < map.Width * 2; i++)
            
{
                
int x = random.Next(map.Width);
                
int y = random.Next(map.Height);
                graph.DrawRectangle(blackPen, x, y, 
11);
            }

        }


        
/// <summary>
        
/// 前景色噪点
        
/// </summary>
        
/// <param name="map"></param>

        private void CreaetForeChaos(Bitmap map)
        
{
            
for (int i = 0; i < map.Width * _chaosNumber; i++)
            
{
                
int x = random.Next(map.Width);
                
int y = random.Next(map.Height);
                
//map.SetPixel(x, y, Color.FromArgb(random.Next(300)));
                map.SetPixel(x, y, _fontColor);
            }

        }



        
/// <summary>
        
/// 创建随机码图片
        
/// </summary>
        
/// <param name="context"></param>

        public void CreateImage(HttpContext context)
        
{
            CreateCode();     
//创建验证码

            Bitmap map 
= new Bitmap((int)(_randomChars.Length * 15), 24);              //创建图片背景
            Graphics graph = Graphics.FromImage(map);
            
//graph.FillRectangle(new SolidBrush(Color.Black), 0, 0, map.Width+1, map.Height+1);     //填充一个有背景的矩形
            
            
//if (_border > 0) //画一个边框
            
//{
            
//    graph.DrawRectangle(new Pen(Color.Black, 0), 0, 0, map.Width - _border, map.Height - _border);
            
//}    
            
//graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;    //模式

            graph.Clear(_backgroundColor);       
//清除画面,填充背景
            SolidBrush brush = new SolidBrush(_fontColor);  //画笔
            Point dot = new Point(1212);

            
//CreateBackgroundChaos(map,graph);       //背景噪点生成

            CreaetForeChaos(map);       
//前景色噪点

            
//文字距中
            StringFormat format = new StringFormat(StringFormatFlags.NoClip);
            format.Alignment 
= StringAlignment.Center;
            format.LineAlignment 
= StringAlignment.Center;

            
//验证码旋转,防止机器识别
            char[] chars = _randomChars.ToCharArray();      //拆散字符串成单字符数组
            for (int i = 0; i < chars.Length; i++)
            
{
                Font fontstyle 
= new Font(_fontFamily, random.Next(_fontSize - 3, _fontSize), FontStyle.Regular);      //字体样式
                float angle = random.Next(-_randomAngle, _randomAngle);      //转动的度数

                graph.TranslateTransform(dot.X, dot.Y);     
//移动光标到指定位置
                graph.RotateTransform(angle);
                graph.DrawString(chars[i].ToString(), fontstyle, brush, 
-22, format);
                graph.RotateTransform(
-angle);          //转回去
                graph.TranslateTransform(2-dot.Y);     //移动光标到指定位置
            }

           

            
//生成图片
            MemoryStream ms = new MemoryStream();
            map.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            context.Response.ClearContent();
            context.Response.ContentType 
= "image/gif";
            context.Response.BinaryWrite(ms.ToArray());
            graph.Dispose();
            map.Dispose();

        }










    }






}

 

 使用方法:

 

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 CS.Common.Utility;

namespace Test
{
    
public partial class Vcode : System.Web.UI.Page
    
{
        
protected void Page_Load(object sender, EventArgs e)
        
{

            VerifyCode vcode 
= new VerifyCode();

            vcode.CreateImage(
base.Context);

            Session[
"VerifyCode"= vcode.RandomCode;

          

        }

    }

}

 

把这个页面当成图片引用就行了!