【asp精品源码栏目提醒】:网学会员--在 asp精品源码编辑为广大网友搜集整理了:一个图片验证码的HtmlHelper - 其它资料绩等信息,祝愿广大网友取得需要的信息,参考学习。
一个图片验证码的 HtmlHelper,原来的调用代码如下:封装成 HtmlHelper 后:Html.ValidateCode使用步骤如下:1.建一个验证码 Helperusing Systemusing System.Collections.Genericusing System.ComponentModelusing Systemusing System.Collections.Genericusing System.Diagnostics.CodeAnalysisusing System.Globalizationusing System.Linq.Expressionsusing System.Security.Policyusing System.Textusing System.Webusing System.Web.Mvcusing System.Web.Mvc.Resourcesusing System.Web.Routingnamespace MvcApplication1public static class ValidateCodeHelperprivate const string IdPrefix validateCodeprivate const int Length 4public static MvcHtmlString ValidateCodethis HtmlHelper helperreturn ValidateCodehelper IdPrefixpublic static MvcHtmlString ValidateCodethis HtmlHelper helperstringidreturn ValidateCodehelper id Lengthpublic static MvcHtmlString ValidateCodethis HtmlHelper helper stringid int lengthreturn ValidateCodehelper id length nullpublic static MvcHtmlString ValidateCodethis HtmlHelper helper stringid object htmlAttributesreturn ValidateCodehelper id Length htmlAttributespublic static MvcHtmlString ValidateCodethis HtmlHelper helper intlength object htmlAttributesreturn ValidateCodehelper IdPrefix length htmlAttributespublic static MvcHtmlString ValidateCodethis HtmlHelper helper objecthtmlAttributesreturn ValidateCodehelper 4 htmlAttributespublic static MvcHtmlString ValidateCodethis HtmlHelper helper intlengthreturn ValidateCodehelperlength nullpublic static MvcHtmlString ValidateCodethis HtmlHelper helperstringidint lengthobject htmlAttributesstring finalId id _imgValidateCodevar tagBuild new TagBuilderimgtagBuild.GenerateIdfinalIdvar defaultController RouteRouteTable.RoutesDefault.Defaultscontroller /var controller HttpContext.Current.Request.Url.Segments.Length 1 defaultController: HttpContext.Current.Request.Url.Segments1tagBuild.MergeAttributesrc string.Format/0GetValidateCodelength1controllerlengthtagBuild.MergeAttributealt 看不清?点我试试看!tagBuild.MergeAttributestylecursor:pointertagBuild.MergeAttributesAnonymousObjectToHtmlAttributeshtmlAttributesvar sb new StringBuildersb.Append return MvcHtmlString.CreatetagBuildsb.ToStringpublic static RouteValueDictionary AnonymousObjectToHtmlAttributesobject htmlAttributesvar result new RouteValueDictionaryif htmlAttributes nullforeach PropertyDescriptor property in TypeDescriptor.GetPropertieshtmlAttributesresult.Addproperty.Name.Replace_ - property.GetValuehtmlAttributesreturn result2.生成验证码的代码:using Systemusing System.Collections.Genericusing System.Linqusing System.Webusing System.Drawingusing System.Drawing.Drawing2Dusing System.IOusing System.Drawing.Imagingnamespace MvcApplication1public class ValidateCodepublic ValidateCode////// 验证码的最大长度///public int MaxLengthget return 10 ////// 验证码的最小长度///public int MinLengthget return 1 ////// 生成验证码//////指定验证码的长度///public string CreateValidateCodeint lengthvar randMembers new intlengthvar validateNums new intlengthvar validateNumberStr //生成起始序列值var seekSeek uncheckedintDateTime.Now.Ticksvar seekRand new RandomseekSeekvar beginSeek intseekRand.Next0 Int32.MaxValue - length 10000var seeks new intlengthfor var i 0 i length ibeginSeek 10000seeksi beginSeek//生成随机数字for var i 0 i length ivar rand new Randomseeksivar pownum 1 intMath.Pow10 lengthrandMembersi rand.Nextpownum Int32.MaxValue//抽取随机数字for var i 0 i length ivar numStr randMembersi.ToStringvar numLength numStr.Lengthvar rand new Randomvar numPosition rand.Next0 numLength - 1validateNumsi Int32.ParsenumStr.SubstringnumPosition 1//生成验证码for var i 0 i length ivalidateNumberStr validateNumsi.ToStringreturn validateNumberStr////// 创建验证码的图片//////验证码public byte CreateValidateGraphicstring validateCodevar image new BitmapintMath.CeilingvalidateCode.Length 12.022var g Graphics.FromImageimagetry//生成随机生成器var random new Random//清空图片背景色g.ClearColor.White//画图片的干扰线for int i 0 i 25 ivar x1 random.Nextimage.Widthvar x2 random.Nextimage.Widthvar y1 random.Nextimage.Heightvar y2 random.Nextimage.Heightg.DrawLinenew PenColor.Silver x1 y1 x2 y2//Font font new FontArial 12 FontStyle.Bold FontStyle.Italicstring fontName 华文新魏 宋体 圆体 黑体 隶书 var font new FontfontNamenew Random.Next0validateCode.Length 12 FontStyle.Bold FontStyle.Italicvar brush new LinearGradientBrushnew Rectangle0 0 image.Widthimage.HeightColor.Blue Color.DarkRed 1.2f trueg.DrawStringvalidateCode font brush 3 2//画图片的前景干扰点for var i 0 i 100 ivar x random.Nextimage.Widthvar y random.Nextimage.Heightimage.SetPixelx y Color.FromArgbrandom.Next//画图片的边框线g.DrawRectanglenew PenColor.Silver 0 0 image.Width - 1image.Height - 1//保存图片数据var stream new MemoryStreamimage.Savestream ImageFormat.Jpeg//输出图片流return stream.ToArrayfinallyg.Disposeimage.Dispose////// 得到验证码图片的长度//////验证码的长度///public static int GetImageWidthint validateNumLengthreturn intvalidateNumLength 12.0////// 得到验证码的高度//////public static double GetImageHeightreturn 233.建一个 BaseControllerusing Systemusing System.Collections.Genericusing System.Linqusing System.Webusing System.Web.Mvcnamespace MvcApplication1public class BaseController:Controllerpublic ActionResult GetValidateCodeint lengthvar vCode new ValidateCodevar code vCode.CreateValidateCodelengthSessionValidateCode codevar bytes vCode.CreateValidateGraphiccodereturn Filebytes image/gifprotected string GetValidateCodereturn SessionValidateCode.ToString4.让 Controller 继承 BaseController:using Systemusing System.Collections.Genericusing System.Linqusing System.Webusing System.Web.Mvcusing System.Web.Securitynamespace MvcApplication1.Controllerspublic class HomeController :BaseControllerpublic ActionResult IndexViewBag.Message Welcome to ASP.NET MVCreturn Viewpublic ActionResult Aboutvar code GetValidateCodereturn View5.页面调用代码:using MvcApplication1ViewBag.Title About UsAboutPut content here.Html.ValidateCode6.验证码的效果图:
上一篇:
某OA建设方案
下一篇:
白色姜花,略微开了