現(xiàn)在越來越多的網(wǎng)站喜歡搞個(gè)驗(yàn)證碼出來,而且各個(gè)語(yǔ)言基本上都能做到,今天我來一個(gè)c#寫的!
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.drawing.imaging;
using system.drawing.drawing2d;
//建立位圖對(duì)象
public void randomnumber()
{
bitmap newbitmap = new bitmap(36,16,pixelformat.format32bppargb);
//根據(jù)上面創(chuàng)建的位圖對(duì)象創(chuàng)建繪圖面
graphics g = graphics.fromimage(newbitmap);
//以指定的顏色填充矩形區(qū)
g.fillrectangle(new solidbrush(color.white), new rectangle(0,0,36,16));
//創(chuàng)建字體對(duì)象
font textfont = new font("times new roman",10);
//創(chuàng)建rectanglef結(jié)構(gòu)指定一個(gè)區(qū)域
rectanglef rectangle = new rectanglef(0,0,36,16);
//創(chuàng)建隨機(jī)數(shù)對(duì)象
random rd = new random();
//取得隨機(jī)數(shù)
int valationno = 1000 + rd.next(8999);
//使用指定的顏色填充上面rectanglef結(jié)構(gòu)指定的矩形區(qū)域
g.fillrectangle(new solidbrush(color.burlywood), rectangle);
//在上面填充的矩形區(qū)域中填充上面生成的隨機(jī)數(shù)
g.drawstring(valationno.tostring(), textfont, new solidbrush(color.blue), rectangle);
//把創(chuàng)建的位圖保存到指定的路徑
newbitmap.save(server.mappath("img")+"http://img.gif", imageformat.gif);
}
生成以后在前臺(tái)頁(yè)面里引入這個(gè)圖片的地址就可以了!
新聞熱點(diǎn)
疑難解答
圖片精選