验证码生成

来源:互联网 发布:艾克里里视频变声软件 编辑:程序博客网 时间:2024/06/05 11:35
 protected void Page_Load(object sender, EventArgs e)        {            string checkCode = createRandomCode(5);//数字为生成验证码所含字符的个数。            Session["checkCode"] = checkCode;            this.createImage(checkCode);        }        private string createRandomCode(int codeCount)        {<span style="white-space:pre"></span>//此处为验证码所含的所有字符            string str = "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,1,2,3,4,5,6,7,8,9,0,A,e,f,R,9,0,K,2,4,7,1,3,q,W,y,u,V,x,p,S,a,D,8,5,T";            string[] codeChar = str.Split(',');            string randomCode = "";            int temp = -1;            Random r = new Random();            for (int i = 0; i < codeCount; i++)            {                if (temp != -1)                {                    r = new Random(i * temp * ((int)DateTime.Now.Ticks));                }                int t = r.Next(87);                if (temp == t)                {                    return createRandomCode(codeCount);                }                temp = t;                randomCode += codeChar[t];            }            Session["CheckCode"] = randomCode;            return randomCode;        }        private void createImage(string randomCode)        {            //创建一个图形,也可以直接定义图片的高和宽            int imageWidth = (int)(randomCode.Length * 20);//20为单个字符宽度            Bitmap image = new Bitmap(imageWidth, 30);//高度            Graphics g = Graphics.FromImage(image);            Random rand = new Random();            //定义颜色            Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };            //定义字体             string[] font = { "Times New Roman", "楷书", "幼圆", "Arial", "宋体" };            //输出不同字体和颜色的验证码字符            g.Clear(Color.White);            for (int i = 0; i < randomCode.Length; i++)            {                int cindex = rand.Next(8);                int findex = rand.Next(5);                int sindex = rand.Next(15, 20);//字体大小,15-20之间随进取数字作为字体的大小。                Font f = new Font(font[findex], sindex, FontStyle.Bold);                Brush b = new SolidBrush(c[cindex]);                //指定每一个字符的格式                int ii = 4;                if ((i + 1) % 2 == 0)                {                    ii = 2;                }                g.DrawString(randomCode.Substring(i, 1), f, b, i * 20, ii);                //第一个参数表示显示的字符,第二、三个参数分别表示字体和颜色,                //第四个参数表示离原点的距离(x坐标),第五个参数表示纵坐标(y坐标)                        }            for (int i = 0; i < 4; i++)            {                int x1 = rand.Next(image.Width);                int x2 = rand.Next(image.Width);                int y1 = rand.Next(image.Height);                int y2 = rand.Next(image.Height);                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);            }            for (int i = 0; i < 100; i++)            {                int x = rand.Next(image.Width);                int y = rand.Next(image.Height);                image.SetPixel(x, y, Color.FromArgb(rand.Next()));            }            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);            MemoryStream ms = new MemoryStream();            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);            Response.ClearContent();            Response.ContentType = "image/Jpeg";            Response.BinaryWrite(ms.ToArray());            g.Dispose();            image.Dispose();        }    }

0 0
原创粉丝点击