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

ValidNumber.ashx驗證碼

2022-06-13   來源: .NET編程 
    ValidNumberashx 代碼
   
    <%@ WebHandler Language=C# Class=ValidNumber %>
   
    using System;
   
    using SystemWeb;
   
    using SystemWebSessionState; //第一步導入此命名空間
   
    public class ValidNumber : IHttpHandler IRequiresSessionState
   
    {
   
    public void ProcessRequest(HttpContext context)
   
    {
   
    //種子
   
    string[] strlist = new[]
   
    {
   
    abcdqwertyuiopzxnvWQBADDFGHJKL
   
    };
   
    //生成隨機數
   
    string[] list = new string[];
   
    Random rd = new Random(DateTimeNowMillisecond *
   
    for (int i = ; i < ; i++)
   
    {
   
    list[i] = strlist[rdNext(strlistLength)];
   
    SystemThreadingThreadSleep(
   
    }
   
    SystemTextStringBuilder sb = new SystemTextStringBuilder()
   
    for (int i = ; i < ; i++)
   
    {
   
    sb = sbAppend(list[i])
   
    }
   
    HttpContextCurrentSession[validNumber] = sb;
   
    //創建一張圖片
   
    SystemDrawingBitmap image = new SystemDrawingBitmap(listLength *
   
    //創建畫布
   
    SystemDrawingGraphics g = SystemDrawingGraphicsFromImage(image)
   
    //清空背景色並添充成白色
   
    gClear(SystemDrawingColorWhite)
   
    //噪聲線
   
    for (int i = ; i < ; i++)
   
    {
   
    Random rds = new Random()
   
    float x = rdsNext(imageWidth
   
    float x = rdsNext(imageWidth
   
    float y = rdsNext(imageHeight
   
    float y = rdsNext(imageHeight
   
    SystemThreadingThreadSleep(
   
    gDrawLine(new SystemDrawingPen(SystemDrawingColorSilver) x y x y
   
    }
   
    SystemDrawingFont f = new SystemDrawingFont(黑體 SystemDrawingGraphicsUnitPixel)
   
    SystemDrawingBrush bs = new SystemDrawingSolidBrush(SystemDrawingColorRed)
   
    gDrawString(sbToString() f bs
   
    //當前請求輸出的類型
   
    HttpContextCurrentResponseContentType = image/jpeg;
   
    //保存到輸出流
   
    imageSave(HttpContextCurrentResponseOutputStream SystemDrawingImagingImageFormatJpeg)
   
    gDispose()
   
    imageDispose()
   
    }
   
    public bool IsReusable
   
    {
   
    get
   
    {
   
    return false;
   
    }
   
    }
   
    }
   
    頁面調用時 <img src=ValidNumberashx alt=圖片驗證碼 /> 但是回發session值改變頁面圖片沒變暫無比較理想的解決辦法提交時後台用  ResponseWrite(<script> windowlocationhref=windowlocationhref</script>)  刷新下頁面
From:http://tw.wingwit.com/Article/program/net/201311/13143.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.