熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET驗證碼(.aspx)

2013-11-13 10:02:15  來源: .NET編程 

  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()}


From:http://tw.wingwit.com/Article/program/net/201311/12346.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.