您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> asp.net编程 >> 正文

随机图片生成器制作方法

作者:佚名    责任编辑:admin    更新时间:2022-06-22

 .net处理程序生成随机6位图片验证码,大家参考使用吧

代码如下: /// <summary>     /// PicHandler1 的摘要说明     /// </summary>     public class PicHandler1 : IHttpHandler, IRequiresSessionState     {         private string mCheckNo = string.Empty;         protected ImgBuilder _ImgBuilder = new ImgBuilder();         protected VryImgGen _ImgBuilderNew = new VryImgGen();         private string _text = string.Empty;         private string _font = "宋体";         private int _fontSize = 8;         private int _padding = 2;         public void ProcessRequest(HttpContext context)         {             mCheckNo = DisCheckNo();             context.Session["CheckCode"] = mCheckNo;             this._ImgBuilder.FontSize = this._fontSize;             this._ImgBuilder.Padding = this._padding;               if (!string.IsNullOrEmpty(this._font))             {                 this._ImgBuilder.Fonts = new string[] { this._font };             }             this._ImgBuilderNew.ChaosWight = 40;             this._ImgBuilderNew.FontSize = 25;               this._ImgBuilderNew.Padding = 3;             System.Drawing.Bitmap image =              this._ImgBuilderNew.CreateImage(mCheckNo);             System.IO.MemoryStream ms = new System.IO.MemoryStream();             image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);             context.Response.ClearContent();             context.Response.ContentType = "image/Jpeg";             context.Response.BinaryWrite(ms.ToArray());             context.Session["CheckCode"] = mCheckNo.ToString(); //如果没有实现IRequiresSessionState,则这里会出错,也无法生成图片             context.Response.End();         }           //验证码生成         protected string DisCheckNo()         {             string hash = HashCode.GetNext();             string CheckNo = string.Empty;             Random rd = new Random(DateTime.Now.Millisecond);             for (int i = 0; i < 6; i++)             {                 CheckNo += hash.Substring(rd.Next(1, hash.Length - 1), 1);             }             CheckNo = CheckNo.Replace("0", rd.Next(1, 9).ToString());             CheckNo = CheckNo.Replace("o", rd.Next(1, 9).ToString());             CheckNo = CheckNo.Replace("O", rd.Next(1, 9).ToString());             return CheckNo;         }           public bool IsReusable         {             get             {                 return false;             }         }     }         调用的类    代码如下: using System; using System.Drawing; using System.Text;   /// <summary> /// VryImgGen 的摘要说明 /// </summary> public class VryImgGen {     public static string ChineseChars = String.Empty;       /// <summary>     /// 英文与数字串     /// </summary>     protected static readonly string EnglishOrNumChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";       public VryImgGen()     {         rnd = new Random(unchecked((int)DateTime.Now.Ticks));     }       /// <summary>     /// 全局随机数生成器     /// </summary>     private Random rnd;       int length = 5;     /// <summary>     /// 验证码长度(默认6个验证码的长度)     /// </summary>     public int Length     {         get { return length; }         set { length = value; }     }       int fontSize = 20;     /// <summary>     /// 验证码字体大小(为了显示扭曲效果,默认30像素,可以自行修改)     /// </summary>     public int FontSize     {         get { return fontSize; }         set { fontSize = value; }     }       int padding = 4;     /// <summary>     /// 边框补(默认4像素)     /// </summary>     public int Padding     {         get { return padding; }         set { padding = value; }     }       bool chaos = true;     /// <summary>     /// 是否输出燥点(默认输出)     /// </summary>     public bool Chaos     {         get { return chaos; }         set { chaos = value; }     }       Color chaosColor = Color.LightGray;     /// <summary>     /// 输出燥点的颜色(默认灰色)     /// </summary>     public Color ChaosColor     {         get { return chaosColor; }         set { chaosColor = value; }     }       int chaosWight = 1;     /// <summary>     /// 输出燥点的浓度     /// </summary>     public int ChaosWight     {         get { return chaosWight; }         set { chaosWight = value; }     }       Color backgroundColor = Color.White;     /// <summary>     /// 自定义背景色(默认白色)     /// </summary>     public Color BackgroundColor     {         get { return backgroundColor; }         set { backgroundColor = value; }     }       Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };     /// <summary>     /// 自定义随机颜色数组     /// </summary>     public Color[] Colors     {         get { return colors; }         set { colors = value; }     }       string[] fonts = { "Arial", "Georgia" };     /// <summary>     /// 自定义字体数组     /// </summary>     public string[] Fonts