aspx生成驗證碼


 //定義方法

public partial class VerificationCode : System.Web.UI.Page
    {
        string ImagePath = "images/validator.jpg";
        string sValidator = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            validate();
        }
        public void validate()
        {
            if (Request.Params["Validator"] != null)
            {
                sValidator = Request.Params["Validator"].ToString();
            }

            ///創建Bmp位圖

            Bitmap bitMapImage = new Bitmap(Server.MapPath(ImagePath));
            Graphics graphicImage = Graphics.FromImage(bitMapImage);

            ///設置畫筆的輸出模式
            graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
            ///添加文本字符串
            graphicImage.DrawString(sValidator, new Font("Arial", 20, FontStyle.Bold), SystemBrushes.WindowText, new Point(0, 0));

            ///設置圖像輸出的格式
            Response.ContentType = "image/jpeg";

            ///保存數據流
            bitMapImage.Save(Response.OutputStream, ImageFormat.Jpeg);

            ///釋放占用的資源
            graphicImage.Dispose();
            bitMapImage.Dispose();
        }
    }

---------------------------------------------------------------------------------------------------------------------------------------------------------

//調用

     public partial class login : System.Web.UI.Page
    {

        static string sValidator = "";
        private readonly string sValidatorImageUrl = "VerificationCode.aspx?Validator=";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                sValidator = GetRandomint();
                ValidateImage.ImageUrl = sValidatorImageUrl + sValidator;

            }
        }


        private String GetRandomint()
        {
            Random random = new Random();
            return (random.Next(100000, 999999).ToString());
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM