国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

收藏一段小的.net下的驗(yàn)證碼片段

2024-07-10 13:09:45
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

商業(yè)源碼熱門(mén)下載www.html.org.cn

收藏一段小的.net下的驗(yàn)證碼片段,以供以后參考。

 protected void page_load(object sender, eventargs e)
    {
         //先產(chǎn)生數(shù)字串
         string checkcode = this.createrandomcode(6);
        //用session保存
         session["checkcode"] = checkcode;
      //作圖
        createimage(checkcode);
       
    }
    private void createimage(string checkcode)
    {
        system.drawing.bitmap image = new system.drawing.bitmap(convert.toint32(math.ceiling((decimal)(checkcode.length * 14))), 22);
        graphics g = graphics.fromimage(image);
       

        try
        {
          
            random random = new random();
            g.clear(color.aliceblue);
           
            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, y1, x2, y2);
            }

            font font = new system.drawing.font("comic sans ms", 12, system.drawing.fontstyle.bold);
            system.drawing.drawing2d.lineargradientbrush brush = new system.drawing.drawing2d.lineargradientbrush(new rectangle(0, 0, image.width, image.height), color.blue, color.darkred, 1.2f, true);
            g.drawstring(checkcode, font, new solidbrush(color.red), 2, 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);

            system.io.memorystream ms = new system.io.memorystream();
            image.save(ms, system.drawing.imaging.imageformat.gif);
            response.clearcontent();
            response.contenttype = "image/gif";
            response.binarywrite(ms.toarray());
        }
        finally
        {
            g.dispose();
            image.dispose();
        }
    }

    public 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";
        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(36);
            if (temp != -1 && temp == t)
            {
                return createrandomcode(codecount);
            }
            temp = t;
            randomcode += allchararray[t];
        }
        return randomcode;
    }

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 清水县| 蓬安县| 武清区| 博野县| 谷城县| 积石山| 咸阳市| 同心县| 洛扎县| 华池县| 梧州市| 屯留县| 夹江县| 拉孜县| 白水县| 阳江市| 谢通门县| 浦江县| 高雄市| 北辰区| 巫山县| 梅河口市| 五河县| 苏尼特右旗| 溧水县| 志丹县| 博野县| 柘荣县| 疏附县| 朔州市| 双鸭山市| 汉源县| 历史| 华容县| 司法| 正定县| 广丰县| 乐清市| 库尔勒市| 元朗区| 澄江县|