oracle .net验证码

来源:互联网 发布:网络视听审核员怎么考 编辑:程序博客网 时间:2024/05/17 03:00

代码:

随机数代码:

private string CreateRandomCode(int codecount)
    {
        string allchar = "0,1,2,3,4,5,6,7,8,9,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,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[] allcharArray = allchar.Split(',');
        string randomCode = "";
        int temp;
        Random rand = new Random();
        for (int i = 0; i < codecount; i++)
        {
            int t = rand.Next(71);
            temp = t;
            randomCode +=allcharArray[t];
        }
        return randomCode;
    }


  干扰图片代码:

  private void CreateImage(string checkcode)
    {
        int iwidth = (int)(checkcode.Length * 30);
        System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth,50);
        Graphics g = Graphics.FromImage(image);
        Font f = new System.Drawing.Font("Arial",25,System.Drawing.FontStyle.Bold);
        Brush b = new System.Drawing.SolidBrush(Color.White);
        g.Clear(Color.Red);
        g.DrawString(checkcode,f,b,3,3);
        Pen blackpen = new Pen(Color.Blue,0);
        Random rand = new Random();
        for (int i = 0; i < 4; i++)
        {
            int y = rand.Next(image.Height);
            g.DrawLine(blackpen,0,y,image.Width,y);
        }
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
        Response.ClearContent();
        Response.ContentType = "image/Jpeg";
        Response.BinaryWrite(ms.ToArray());
        g.Dispose();
        image.Dispose();
       
    }

 

调用上面的两个方法的page_load

 protected void Page_Load(object sender, EventArgs e)
    {
        string checkcode = CreateRandomCode(4);
        Session["checkcode"] = checkcode;
        CreateImage(checkcode);
    }