zoukankan      html  css  js  c++  java
  • 验证码(转)


    把下面代码存为一个文件code.aspx。另一个文件里调用<img src="code.aspx">


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

    public partial class Default4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                getimg(getcode(4));
            }
        }


        public string getcode(int num)
        {

            string[] source ={ "1", "A", "B", "4", "C", "6", "7", "8", "9", "Q", "W", "Y", "U", "O", "P" };
            //string[] source ={ "A", "B", "C", "Q", "W", "Y", "U", "O", "P" };
            string code = "";
            Random rm = new Random();
            for(int i = 0; i < num; i++)
            {
                code += source[rm.Next(0, source.Length)];
            }
            return code;
        }


        public void getimg(string code)
        {
            Bitmap myPalette = new Bitmap(60, 20);
            Graphics gh = Graphics.FromImage(myPalette);
            Rectangle rc = new Rectangle(0, 0, 60, 20);
            gh.FillRectangle(new SolidBrush(Color.Silver), rc);
            gh.DrawString(code, new Font("宋体", 16), new SolidBrush(Color.Gray), rc);
            myPalette.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            Session["ValidataCode"] = code;
            gh.Dispose();
            myPalette.Dispose();

        }
    }

  • 相关阅读:
    vue 同时使用过渡和动画
    ie和火狐事件addEventListener()及attachEvent()区别分析
    闭包的理解
    实现Date函数属性中的format方法
    js读取cookie信息
    js解决千分符问题
    js脚本语言在页面上不执行
    split使用和特殊使用(包括截取第一个字符后的数据)
    js中请求数据的$post和$ajax区别(同步和异步问题)
    禁止页面拖拽事件(数据什么的)
  • 原文地址:https://www.cnblogs.com/eebb/p/710888.html
Copyright © 2011-2022 走看看