推荐一个不错的验证码[C#]

 效果,看这里!! 

以下是全部代码:
  1 ContractedBlock.gif ExpandedBlockStart.gif source #region source
  2InBlock.gifusing System;
  3InBlock.gifusing System.Collections;
  4InBlock.gifusing System.ComponentModel;
  5InBlock.gifusing System.Data;
  6InBlock.gifusing System.Drawing;
  7InBlock.gifusing System.Web;
  8InBlock.gifusing System.Web.SessionState;
  9InBlock.gifusing System.Web.UI;
 10InBlock.gifusing System.Web.UI.WebControls;
 11InBlock.gifusing System.Web.UI.HtmlControls;
 12ExpandedBlockEnd.gif#endregion

 13 None.gif
 14 None.gif namespace  ListNews
 15 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 16ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 17InBlock.gif    /// ValidateCode 的摘要说明。
 18ExpandedSubBlockEnd.gif    /// </summary>

 19InBlock.gif    public class ValidateCode : System.Web.UI.Page
 20ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 21ContractedSubBlock.gifExpandedSubBlockStart.gif        页面载入#region 页面载入
 22InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 24InBlock.gif            CreateCheckCodeImage(GenerateCheckCode(4));
 25ExpandedSubBlockEnd.gif        }

 26ExpandedSubBlockEnd.gif        #endregion
 
 27InBlock.gif
 28ContractedSubBlock.gifExpandedSubBlockStart.gif        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
 29InBlock.gif        override protected void OnInit(EventArgs e)
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 31InBlock.gif            //
 32InBlock.gif            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
 33InBlock.gif            //
 34InBlock.gif            InitializeComponent();
 35InBlock.gif            base.OnInit(e);
 36ExpandedSubBlockEnd.gif        }

 37InBlock.gif        
 38ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 39InBlock.gif        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 40InBlock.gif        /// 此方法的内容。
 41ExpandedSubBlockEnd.gif        /// </summary>

 42InBlock.gif        private void InitializeComponent()
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
 44InBlock.gif            this.Load += new System.EventHandler(this.Page_Load);
 45ExpandedSubBlockEnd.gif        }

 46ExpandedSubBlockEnd.gif        #endregion

 47InBlock.gif
 48ContractedSubBlock.gifExpandedSubBlockStart.gif        Method GenerateCheckCode#region Method GenerateCheckCode
 49ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 50InBlock.gif        /// 
 51InBlock.gif        /// </summary>
 52InBlock.gif        /// <param name="codeCount"></param>
 53ExpandedSubBlockEnd.gif        /// <returns></returns>

 54InBlock.gif        private string GenerateCheckCode(int codeCount)
 55ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 56InBlock.gif            int number;
 57InBlock.gif            char code;
 58InBlock.gif            string checkCode = String.Empty;
 59InBlock.gif
 60InBlock.gif            System.Random random = new Random();
 61InBlock.gif
 62InBlock.gif            for(int i=0; i<codeCount; i++)
 63ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 64InBlock.gif                number = random.Next();
 65InBlock.gif
 66InBlock.gif                if(number % 2 == 0)
 67InBlock.gif                    code = (char)('0' + (char)(number % 10));
 68InBlock.gif                else
 69InBlock.gif                    code = (char)('A' + (char)(number % 26));
 70InBlock.gif
 71InBlock.gif                checkCode += code.ToString();
 72ExpandedSubBlockEnd.gif            }

 73InBlock.gif
 74InBlock.gif            //Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
 75InBlock.gif
 76InBlock.gif            return checkCode;
 77ExpandedSubBlockEnd.gif        }

 78InBlock.gif
 79ExpandedSubBlockEnd.gif        #endregion

 80InBlock.gif
 81ContractedSubBlock.gifExpandedSubBlockStart.gif        Method CreateCheckCodeImage#region Method CreateCheckCodeImage
 82ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 83InBlock.gif        /// 
 84InBlock.gif        /// </summary>
 85ExpandedSubBlockEnd.gif        /// <param name="checkCode"></param>

 86InBlock.gif        private void CreateCheckCodeImage(string checkCode)
 87ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 88InBlock.gif            if(checkCode == null || checkCode.Trim() == String.Empty)
 89InBlock.gif                return;
 90InBlock.gif
 91InBlock.gif            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
 92InBlock.gif            Graphics g = Graphics.FromImage(image);
 93InBlock.gif
 94InBlock.gif            try
 95ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 96InBlock.gif                //生成随机生成器
 97InBlock.gif                Random random = new Random();
 98InBlock.gif
 99InBlock.gif                //清空图片背景色
100InBlock.gif                g.Clear(Color.White);
101InBlock.gif
102InBlock.gif                //画图片的背景噪音线
103InBlock.gif                for(int i=0; i<25; i++)
104ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
105InBlock.gif                    int x1 = random.Next(image.Width);
106InBlock.gif                    int x2 = random.Next(image.Width);
107InBlock.gif                    int y1 = random.Next(image.Height);
108InBlock.gif                    int y2 = random.Next(image.Height);
109InBlock.gif
110InBlock.gif                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
111ExpandedSubBlockEnd.gif                }

112InBlock.gif
113InBlock.gif                Font font = new System.Drawing.Font("Arial"12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
114InBlock.gif                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(00, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2ftrue);
115InBlock.gif                g.DrawString(checkCode, font, brush, 22);
116InBlock.gif
117InBlock.gif                //画图片的前景噪音点
118InBlock.gif                for(int i=0; i<100; i++)
119ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
120InBlock.gif                    int x = random.Next(image.Width);
121InBlock.gif                    int y = random.Next(image.Height);
122InBlock.gif
123InBlock.gif                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
124ExpandedSubBlockEnd.gif                }

125InBlock.gif
126InBlock.gif                //画图片的边框线
127InBlock.gif                g.DrawRectangle(new Pen(Color.Silver), 00, image.Width - 1, image.Height - 1);
128InBlock.gif
129InBlock.gif                System.IO.MemoryStream ms = new System.IO.MemoryStream();
130InBlock.gif                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
131InBlock.gif                Response.ClearContent();
132InBlock.gif                Response.ContentType = "image/Gif";
133InBlock.gif                Response.BinaryWrite(ms.ToArray());
134ExpandedSubBlockEnd.gif            }

135InBlock.gif            finally
136ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
137InBlock.gif                g.Dispose();
138InBlock.gif                image.Dispose();
139ExpandedSubBlockEnd.gif            }

140ExpandedSubBlockEnd.gif        }

141ExpandedSubBlockEnd.gif        #endregion

142ExpandedSubBlockEnd.gif    }

143ExpandedBlockEnd.gif}

144 None.gif

转载于:https://www.cnblogs.com/an_andy/archive/2006/03/21/354896.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值