Asp.net(C#)实现验证码功能代码
程序员文章站
2022-04-17 13:26:18
新建一个专门用来创建验证码图片的页面validatecode.aspx 它的后台cs文件代码如下: pageload 复制代码 代码如下:private void page...
新建一个专门用来创建验证码图片的页面validatecode.aspx
它的后台cs文件代码如下:
pageload
private void page_load(object sender, system.eventargs e)
{
string checkcode = createrandomcode(4);
session["checkcode"] = checkcode;
createimage(checkcode);
}
其中createrandomcode是自定义的函数,参数代表验证码位数
private 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,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;
}
createimage也是一个自定义的函数,用于生成图
private void createimage(string checkcode)
{
int iwidth = (int)(checkcode.length * 11.5);
system.drawing.bitmap image = new system.drawing.bitmap(iwidth, 20);
graphics g = graphics.fromimage(image);
font f = new system.drawing.font("arial", 10, system.drawing.fontstyle.bold);
brush b = new system.drawing.solidbrush(color.white);
//g.fillrectangle(new system.drawing.solidbrush(color.blue),0,0,image.width, image.height);
g.clear(color.blue);
g.drawstring(checkcode, f, b, 3, 3);
pen blackpen = new pen(color.black, 0);
random rand = new random();
for (int i=0;i<5;i++)
{
int y = rand.next(image.height);
g.drawline(blackpen,0,y,image.width,y);
}
system.io.memorystream ms = new system.io.memorystream();
image.save(ms,system.drawing.imaging.imageformat.jpeg);
response.clearcontent();
response.contenttype = "image/jpeg";
response.binarywrite(ms.toarray());
g.dispose();
image.dispose();
}
//g.fillrectangle(new system.drawing.solidbrush(color.blue),0,0,image.width, image.height);
g.clear(color.blue);
这两种方法都可以改变生成图片的背景颜色。下面那个for循环用来生成一些随机的水平线
在需要用到验证码的页面添加一个<asp:image>控件即可,但是要把imageurl指向生成验证码的页面
<asp:image runat="server" id="imagecheck" imageurl="validatecode.aspx"></asp:image>
它的后台cs文件代码如下:
pageload
复制代码 代码如下:
private void page_load(object sender, system.eventargs e)
{
string checkcode = createrandomcode(4);
session["checkcode"] = checkcode;
createimage(checkcode);
}
其中createrandomcode是自定义的函数,参数代表验证码位数
复制代码 代码如下:
private 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,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;
}
createimage也是一个自定义的函数,用于生成图
复制代码 代码如下:
private void createimage(string checkcode)
{
int iwidth = (int)(checkcode.length * 11.5);
system.drawing.bitmap image = new system.drawing.bitmap(iwidth, 20);
graphics g = graphics.fromimage(image);
font f = new system.drawing.font("arial", 10, system.drawing.fontstyle.bold);
brush b = new system.drawing.solidbrush(color.white);
//g.fillrectangle(new system.drawing.solidbrush(color.blue),0,0,image.width, image.height);
g.clear(color.blue);
g.drawstring(checkcode, f, b, 3, 3);
pen blackpen = new pen(color.black, 0);
random rand = new random();
for (int i=0;i<5;i++)
{
int y = rand.next(image.height);
g.drawline(blackpen,0,y,image.width,y);
}
system.io.memorystream ms = new system.io.memorystream();
image.save(ms,system.drawing.imaging.imageformat.jpeg);
response.clearcontent();
response.contenttype = "image/jpeg";
response.binarywrite(ms.toarray());
g.dispose();
image.dispose();
}
//g.fillrectangle(new system.drawing.solidbrush(color.blue),0,0,image.width, image.height);
g.clear(color.blue);
在需要用到验证码的页面添加一个<asp:image>控件即可,但是要把imageurl指向生成验证码的页面
复制代码 代码如下:
<asp:image runat="server" id="imagecheck" imageurl="validatecode.aspx"></asp:image>
上一篇: 和尚打伞及解释
下一篇: 公交车上一男一女商量去开房