Mr.Crazy

一切隨緣,戰勝自己。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
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 Univer_Sky { /************************************************** * FileName: CheckCode * Version: 1.00 * Author: Universe 苍天穹缘 * Date: 2006-4-15 * Last Modied Date: 2006-4-15 * Function:验证码 * CopyRight (c) Universe Company 2005-2006 * All rights reserved **************************************************/ public class CheckCode : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 this.CreateCheckCodeImage(GenerateCheckCode()); } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion string y; private string GenerateCheckCode() { //获取数字和字母 // int number; // char code; // string checkCode = String.Empty; // // System.Random random = new Random(); // // for(int i=0; i<5; i++) // { // number = random.Next(); // // if(number % 2 == 0) // code = (char)('0' + (char)(number % 10)); // else // code = (char)('A' + (char)(number % 26)); // // checkCode += code.ToString(); // } //获取中文 //获取GB2312编码页(表) Encoding gb = Encoding.GetEncoding("gb2312"); //调用函数产生4个随机中文汉字编码 object[] bytes = CreateRegionCode(4); //根据汉字编码的字节数组解码出中文汉字 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 checkCode =str1 + str2 + str3 + str4; Response.Cookies.Add(new HttpCookie("CheckCode", checkCode)); 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 * 21)), 22); Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); //画图片的背景噪音线 for(int i=0; i<15; 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("Arial", 12, (System.Drawing.FontStyle.Regular | System.Drawing.FontStyle.Italic)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1f, false); g.DrawString(checkCode, font, brush, 2, 2); //画图片的前景噪音点 for(int i=0; i<30; 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), 0, 0, 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(); } } /**/ /* 此函数在汉字编码范围内随机创建含两个元素的十六进制字节数组,每个字节数组代表一个汉字,并将 四个字节数组存储在object数组中。 参数:strlength,代表需要产生的汉字个数 */ 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
posted on 2007-06-12 10:17  JonyJaJa  阅读(396)  评论(0编辑  收藏  举报