欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#生成验证码

程序员文章站 2024-01-04 09:42:10
/// /// 生成随机的字符串 /// /// 验证码长度 /// public string CreateRandomCode(int codeCount) { string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D... ......
        /// <summary>
        /// 生成随机的字符串
        /// </summary>
        /// <param name="codecount">验证码长度</param>
        /// <returns></returns>
        public string createrandomcode(int codecount)
        {
            string allchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,a,b,c,d,e,f,g,h,i,g,k,l,m,n,o,p,q,r,f,g,h,i,g,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,s,t,u,v,w,x,y,z";
            string[] allchararray = allchar.split(',');
            string randomcode = "";
            int temp = -1;
            random rand = new random();
            for (int i = 0; i < codecount; i++)
            {
                if (temp != -1)
                {
                    rand = new random(i * temp * ((int)datetime.now.ticks));
                }
                int t = rand.next(35);
                if (temp == t)
                {
                    return createrandomcode(codecount);
                }
                temp = t;
                randomcode += allchararray[t];
            }
            return randomcode;
        }

        /// <summary>
        /// 创建验证码图片
        /// </summary>
        /// <param name="validatecode"></param>
        /// <returns></returns>
        public void createvalidategraphic(string validatecode, httpcontext context)
        {
            bitmap image = new bitmap((int)math.ceiling(validatecode.length * 16.0), 27);
            graphics g = graphics.fromimage(image);
            try
            {
                //生成随机生成器
                random random = new random();
                //清空图片背景色
                g.clear(color.white);
                //画图片的干扰线
                for (int i = 0; i < 25; i++)
                {
                    int x1 = random.next(image.width);
                    int x2 = random.next(image.width);
                    int y1 = random.next(image.height);
                    int y2 = random.next(image.height);
                    g.drawline(new pen(color.silver), x1, x2, y1, y2);
                }
                font font = new font("arial", 13, (fontstyle.bold | fontstyle.italic));
                lineargradientbrush brush = new lineargradientbrush(new rectangle(0, 0, image.width, image.height), color.blue, color.darkred, 1.2f, true);
                g.drawstring(validatecode, font, brush, 3, 2);

                //画图片的前景干扰线
                for (int i = 0; i < 100; i++)
                {
                    int x = random.next(image.width);
                    int y = random.next(image.height);
                    image.setpixel(x, y, color.fromargb(random.next()));
                }
                //画图片的边框线
                g.drawrectangle(new pen(color.silver), 0, 0, image.width - 1, image.height - 1);

                //保存图片数据 
                image.save(context.response.outputstream, imageformat.jpeg);

            }
            finally
            {
                g.dispose();
                image.dispose();
            }
        }

 

上一篇:

下一篇: