C# 生成验证码图片
来源:TAG:C# 浏览数: 日期:2010-7-18评论:
折叠C# Code复制内容到剪贴板
  1. using System.Collections.Generic;       
  2. using System.Text;       
  3. using System.Threading;       
  4. using System.Drawing;       
  5. using System.Drawing.Drawing2D;        
  6. namespace FreeOH.Validate       
  7. {       
  8.     /// <summary>       
  9.     /// 本类用于产生随机的验证码       
  10.     /// </summary>       
  11.     public class ValidationCode       
  12.     {       
  13.         //用户存取验证码字符串       
  14.         public string validationCode = String.Empty;        
  15.       
  16.         Graphics g = null;        
  17.       
  18.         int bgWidth = 0;       
  19.         int bgHeight = 0;        
  20.       
  21.         public string FontFace = "Comic Sans MS";       
  22.         public int FontSize = 9;       
  23.         public Color foreColor = Color.FromArgb(220, 220, 220);       
  24.         public Color backColor = Color.FromArgb(190, 190, 190);       
  25.         public Color mixedLineColor = Color.FromArgb(220, 220, 220);       
  26.         public int mixedLineWidth = 1;       
  27.         public int mixedLineCount = 5;      
  28.     
  29.     
  30.         #region 根据指定长度,返回随机验证码       
  31.         /// <summary>       
  32.         /// 根据指定长度,返回随机验证码       
  33.         /// </summary>       
  34.         /// <param name="length">制定长度</param>       
  35.         /// <returns>随即验证码</returns>       
  36.         public string Next(int length)       
  37.         {       
  38.             this.validationCode = GetRandomCode(length);       
  39.             return this.validationCode;       
  40.         }     
  41.         #endregion      
  42.     
  43.     
  44.         #region 根据指定长度及背景图片样式,返回带有随机验证码的图片对象       
  45.         /// <summary>       
  46.         /// 根据指定长度及背景图片样式,返回带有随机验证码的图片对象       
  47.         /// </summary>       
  48.         /// <param name="length">指定长度</param>       
  49.         /// <param name="hatchStyle">背景图片样式</param>       
  50.         /// <returns>Image对象</returns>       
  51.         public Image NextImage(int length, HatchStyle hatchStyle, bool allowMixedLines)       
  52.         {       
  53.             this.validationCode = GetRandomCode(length);        
  54.       
  55.             //校验码字体       
  56.             Font myFont = new Font(FontFace, FontSize);        
  57.       
  58.             //根据校验码字体大小算出背景大小       
  59.             bgWidth = (int)myFont.Size * length + 4;       
  60.             bgHeight = (int)myFont.Size * 2;       
  61.             //生成背景图片       
  62.             Bitmap myBitmap = new Bitmap(bgWidth, bgHeight);        
  63.       
  64.             g = Graphics.FromImage(myBitmap);        
  65.       
  66.       
  67.             this.DrawBackground(hatchStyle);       
  68.             this.DrawValidationCode(this.validationCode,myFont);       
  69.             if (allowMixedLines)       
  70.                 this.DrawMixedLine();        
  71.       
  72.             return (Image)myBitmap;       
  73.         }     
  74.         #endregion      
  75.     
  76.     
  77.         #region 内部方法:绘制验证码背景       
  78.         private void DrawBackground(HatchStyle hatchStyle)       
  79.         {       
  80.             //设置填充背景时用的笔刷       
  81.             HatchBrush hBrush = new HatchBrush(hatchStyle, backColor);        
  82.       
  83.             //填充背景图片       
  84.             g.FillRectangle(hBrush, 0, 0, this.bgWidth, this.bgHeight);       
  85.         }     
  86.         #endregion      
  87.     
  88.     
  89.         #region 内部方法:绘制验证码       
  90.         private void DrawValidationCode(string vCode, Font font)       
  91.         {       
  92.             g.DrawString(vCode, font, new SolidBrush(this.foreColor), 2, 2);       
  93.         }     
  94.         #endregion      
  95.     
  96.     
  97.         #region 内部方法:绘制干扰线条       
  98.         /// <summary>       
  99.         /// 绘制干扰线条       
  100.         /// </summary>       
  101.         private void DrawMixedLine()       
  102.         {       
  103.             for (int i = 0; i < mixedLineCount; i++)       
  104.             {       
  105.                 g.DrawBezier(new Pen(new SolidBrush(mixedLineColor), mixedLineWidth), RandomPoint(), RandomPoint(), RandomPoint(), RandomPoint());       
  106.             }       
  107.         }     
  108.         #endregion      
  109.     
  110.         #region 内部方法:返回指定长度的随机验证码字符串       
  111.         /// <summary>       
  112.         /// 根据指定大小返回随机验证码       
  113.         /// </summary>       
  114.         /// <param name="length">字符串长度</param>       
  115.         /// <returns>随机字符串</returns>       
  116.         private string GetRandomCode(int length)       
  117.         {       
  118.             StringBuilder sb = new StringBuilder(6);        
  119.       
  120.             for (int i = 0; i < length; i++)       
  121.             {       
  122.                 sb.Append(Char.ConvertFromUtf32(RandomAZ09()));       
  123.             }        
  124.       
  125.             return sb.ToString();       
  126.         }     
  127.         #endregion      
  128.     
  129.     
  130.         #region 内部方法:产生随机数和随机点        
  131.       
  132.         /// <summary>       
  133.         /// 产生0-9A-Z的随机字符代码       
  134.         /// </summary>       
  135.         /// <returns>字符代码</returns>       
  136.         private int RandomAZ09()       
  137.         {       
  138.             Thread.Sleep(15);       
  139.             int result = 48;       
  140.             Random ram = new Random();       
  141.             int i = ram.Next(2);        
  142.       
  143.             switch (i)       
  144.             {       
  145.                 case 0:       
  146.                     result = ram.Next(48, 58);       
  147.                     break;       
  148.                 case 1:       
  149.                     result = ram.Next(65, 91);       
  150.                     break;       
  151.             }        
  152.       
  153.             return result;       
  154.         }        
  155.       
  156.         /// <summary>       
  157.         /// 返回一个随机点,该随机点范围在验证码背景大小范围内       
  158.         /// </summary>       
  159.         /// <returns>Point对象</returns>       
  160.         private Point RandomPoint()       
  161.         {       
  162.             Thread.Sleep(15);       
  163.             Random ram = new Random();       
  164.             Point point = new Point(ram.Next(this.bgWidth), ram.Next(this.bgHeight));       
  165.             return point;       
  166.         }     
  167.         #endregion       
  168.     }       
  169. }       
  170.       
  171.         
  172.       
  173. 把图片输入到页面       
  174.       
  175.     System.IO.MemoryStream ms = new System.IO.MemoryStream();       
  176.     image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);       
  177.     System.Web.HttpContext.Current.Response.ClearContent();       
  178.     System.Web.HttpContext.Current.Response.ContentType = "image/Gif";       
  179.     System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());       
  180.       
  181. 生成验证码 点击可刷新       
  182.       
  183. 先建一张生成验证码的网页CheckCode.aspx:CheckCode.aspx.cs代码如下添加引用:using System.IO;using System.Drawing;using System.Drawing.Imaging;using System.Drawing.Drawing2D;然后是显示验证码的页面 的html中加入<img src="Default2.aspx" id="yzm" alt="点击刷新" />在</head>之前一行加入javascript脚本语言如下(当然写在JS文件里面更好):<script type="text/javascript">function reloadcode(){ document.getElementById("yzm").src ="CheckCode.aspx";}       
昨天新闻点击排行
一周新闻点击排行
当月新闻点击排行
新闻链接
上一篇文章:没有下一篇文章了
相关评论
正在加载评论列表...
评论表单加载中...