随机生成6位图片验证码

时间:2017-01-08 11:23:42
【文件属性】:
文件名称:随机生成6位图片验证码
文件大小:11KB
文件格式:TXT
更新时间:2017-01-08 11:23:42
验证码 随机生成6位图片验证码. /// /// PicHandler1 的摘要说明 /// 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());

网友评论