当前位置:编程学习 > C#/ASP.NET >>

ASP.NET 生成图片验证码

新建一个 GetCode.aspx  页面,在 GetCode.aspx.cs 代码文件中粘上下列代码

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Drawing;
using System.Drawing.Drawing2D;

public partial class WebControl_GetCode : System.Web.UI.Page
{
    const int number = 5;
    string checkCode = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        checkCode = "";
        DrawStr(RandomString());
        Session["GetCode"] = checkCode;
    }

    //用于生成随机的字母和数字
    private string RandomString()
    {
        string str = "2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";
        string[] strArray = str.Split('','');
        string codeStr = "";
        Random ran = new Random();
        string s = "";
        for (int i = 0; i < number; i++)
        {
            s = strArray[ran.Next(30)];
            checkCode += s;
            codeStr += " " + s;
        }
        codeStr += " ";
        return codeStr;
    }

    //画图
    private void DrawStr(string codeStr)
补充:asp.net教程,.Net开发

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,