国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 固安县| 攀枝花市| 湛江市| 屏边| 聂拉木县| 九龙城区| 新营市| 中阳县| 桑植县| 淳安县| 淮南市| 定远县| 淳安县| 汉沽区| 宣化县| 临汾市| 延长县| 怀来县| 奉化市| 泾源县| 马山县| 丁青县| 安远县| 林芝县| 扎鲁特旗| 金川县| 兴隆县| 凤城市| 德钦县| 榆林市| 铜川市| 大埔县| 万盛区| 邹平县| 鹤山市| 汽车| 博兴县| 县级市| 吉木乃县| 专栏| 顺义区|