asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

ASP.NET验证码(.aspx)


发布日期:2019年07月03日
 
ASP.NET验证码(.aspx)

using Systemusing SystemCollectionsGenericusing SystemLinqusing SystemWebusing SystemWebUIusing SystemWebUIWebControlsusing SystemDrawingusing SystemDrawingDrawingD //引用的命名控件public partial class ValidCodeImage SystemWebUIPage { protected void Page_Load(object sender EventArgs e)

{ CreateCheckCodeImage(GenCode())} /// <summary> /// 产生随机字符串/// </summary> /// <param name=num>随机出几个字符</param> /// <returns>随机出的字符串</returns> private string GenCode(int num)

{ string str = ABCDEFGHIJKLMNOPQRSTUVWSYZchar[] chastr = strToCharArray()// string[] source ={ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z # $ % & @ }string code = Random rd = new Random()int ifor (i = i < num i++)

{ //code += source[rdNext( sourceLength)]code += strSubstring(rdNext( strLength) } return code} /// <summary> /// 生成图片(增加背景噪音线前景噪音点)

/// </summary> /// <param name=checkCode>随机出字符串</param> private void CreateCheckCodeImage(string checkCode)

{ if (checkCodeTrim() == || checkCode == null)

returnSession[Code] = checkCode //将字符串保存到Session中以便需要时进行验证SystemDrawingBitmap image = new SystemDrawingBitmap((int)(checkCodeLength * Graphics g = GraphicsFromImage(image)try { //生成随机生成器Random random = new Random()//清空图片背景色gClear(ColorWhite)// 画图片的背景噪音线int ifor (i = i < i++)

{ int x = randomNext(imageWidth)int x = randomNext(imageWidth)int y = randomNext(imageHeight)int y = randomNext(imageHeight)gDrawLine(new Pen(ColorSilver) x y x y} Font font = new SystemDrawingFont(Arial (SystemDrawingFontStyleBold))SystemDrawingDrawingDLinearGradientBrush brush = new SystemDrawingDrawingDLinearGradientBrush(new Rectangle( imageWidth imageHeight) ColorBlue ColorDarkRed F true)gDrawString(checkCode font brush //画图片的前景噪音点gDrawRectangle(new Pen(ColorSilver) imageWidth imageHeight SystemIOMemoryStream ms = new SystemIOMemoryStream()imageSave(ms SystemDrawingImagingImageFormatGif)ResponseClearContent()ResponseContentType = image/GifResponseBinaryWrite(msToArray())} catch { gDispose()imageDispose()}

               

上一篇:asp.net教程-委托(Delegate)

下一篇:利用ASP.net编写发送Email程序