c# .net 生成图片验证码的代码
程序员文章站
2023-11-27 12:42:52
说明: .net 万岁... .net framework 的类库真是太强了, 用 gdi+&nb...
说明:
.net 万岁...
.net framework 的类库真是太强了, 用 gdi+ 可以干n多n多事情.
广告时间:
shawl.qiu c# cms 系统 预计40天后开始编码, 现在逐步设计中, 免得到时求职说什么什么作品...唉.
ps: 今天求职真是惨不忍睹, 谁要招网页相关的请联系 13435580019, 邱先生.
什么地方俺都去, 工资只要能过活就行, 但是食宿问题得解决.
shawl.qiu
2007-02-01
http://blog.csdn.net/btbtd
class checkcode:
<%@ page language="c#" autoeventwireup="true" %>
<%@ import namespace="system.drawing"%>
<%@ import namespace="system.drawing.drawing2d"%>
<%@ import namespace="system.web"%>
<script runat="server">
private void page_load(object sender, system.eventargs e)
{
string srndstr=checkcode.rndstr(4);
checkcode.general(srndstr);
}
/*-----------------------------------------------------------------------------------*\
* shawl.qiu c# .net checkcode class v1.0
\*-----------------------------------------------------------------------------------*/
//---------------------------------------------------------------------begin class checkcode
public class checkcode
{
//-----------------------------------begin event
public checkcode()
{
}
~checkcode()
{
}
//-----------------------------------end event
//-----------------------------------begin public constant
//-----------------------begin about
public const string ausubject="shawl.qiu c# .net checkcode class";
public const string auversion="v1.0";
public const string au="shawl.qiu";
public const string auemail="shawl.qiu@gmail.com";
public const string aublog="http://blog.csdn.net/btbtd";
public const string aucreatedate="2007-2-1";
//-----------------------end about
//-----------------------------------end public constant
//-----------------------------------begin public static method
public static void general(string scc)
{
int32 cclen=scc.length;
string ccftfm="arial";
int32 ccftsz=12;
int32 ccwidth=cclen*ccftsz+1;
int32 ccheight=ccftsz+5;
using(bitmap oimg = new bitmap(ccwidth, ccheight))
{
using(graphics ogpc=graphics.fromimage(oimg))
{
hatchbrush hbrush = new hatchbrush(hatchstyle.dashedvertical,
color.yellow, color.silver);
ogpc.fillrectangle(hbrush, 0, 0, ccwidth, ccwidth);
ogpc.drawstring(scc,new system.drawing.font(ccftfm,ccftsz, fontstyle.bold),
new system.drawing.solidbrush(color.black),0,0);
//-----------------------边框
pen blackpen = new pen(color.black, 1);
ogpc.drawline(blackpen, 0, ccheight, 0, 0); // 左竖线
ogpc.drawline(blackpen, 0,0,ccwidth,0); // 顶横线
ogpc.drawline(blackpen, ccwidth-1,0,ccwidth-1,20); // 右竖线
ogpc.drawline(blackpen, 0, ccheight-1, ccwidth, ccheight-1); // 底横线
writeimg(oimg);
}
}
} // end public static void general
public static string rndstr(int32 len)
{
string stemp="";
string sforrnd="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[] arnd=sforrnd.split(',');
random ornd=new random();
int32 iarlen=arnd.length;
for(int32 i=0; i<len; i++)
{
stemp+=arnd[ornd.next(0,iarlen)];
}
return stemp;
} // end public static string rndstr
//-----------------------------------end public static method
//-----------------------------------begin private static method
private static void writeimg(bitmap oimg)
{
using(system.io.memorystream ms=new system.io.memorystream())
{
oimg.save(ms,system.drawing.imaging.imageformat.png);
httpcontext.current.response.clearcontent();
httpcontext.current.response.contenttype="image/png";
httpcontext.current.response.binarywrite(ms.toarray());
}
} // end private static void writeimg
}
//---------------------------------------------------------------------end class checkcode
</script>
.net 万岁...
.net framework 的类库真是太强了, 用 gdi+ 可以干n多n多事情.
广告时间:
shawl.qiu c# cms 系统 预计40天后开始编码, 现在逐步设计中, 免得到时求职说什么什么作品...唉.
ps: 今天求职真是惨不忍睹, 谁要招网页相关的请联系 13435580019, 邱先生.
什么地方俺都去, 工资只要能过活就行, 但是食宿问题得解决.
shawl.qiu
2007-02-01
http://blog.csdn.net/btbtd
class checkcode:
复制代码 代码如下:
<%@ page language="c#" autoeventwireup="true" %>
<%@ import namespace="system.drawing"%>
<%@ import namespace="system.drawing.drawing2d"%>
<%@ import namespace="system.web"%>
<script runat="server">
private void page_load(object sender, system.eventargs e)
{
string srndstr=checkcode.rndstr(4);
checkcode.general(srndstr);
}
/*-----------------------------------------------------------------------------------*\
* shawl.qiu c# .net checkcode class v1.0
\*-----------------------------------------------------------------------------------*/
//---------------------------------------------------------------------begin class checkcode
public class checkcode
{
//-----------------------------------begin event
public checkcode()
{
}
~checkcode()
{
}
//-----------------------------------end event
//-----------------------------------begin public constant
//-----------------------begin about
public const string ausubject="shawl.qiu c# .net checkcode class";
public const string auversion="v1.0";
public const string au="shawl.qiu";
public const string auemail="shawl.qiu@gmail.com";
public const string aublog="http://blog.csdn.net/btbtd";
public const string aucreatedate="2007-2-1";
//-----------------------end about
//-----------------------------------end public constant
//-----------------------------------begin public static method
public static void general(string scc)
{
int32 cclen=scc.length;
string ccftfm="arial";
int32 ccftsz=12;
int32 ccwidth=cclen*ccftsz+1;
int32 ccheight=ccftsz+5;
using(bitmap oimg = new bitmap(ccwidth, ccheight))
{
using(graphics ogpc=graphics.fromimage(oimg))
{
hatchbrush hbrush = new hatchbrush(hatchstyle.dashedvertical,
color.yellow, color.silver);
ogpc.fillrectangle(hbrush, 0, 0, ccwidth, ccwidth);
ogpc.drawstring(scc,new system.drawing.font(ccftfm,ccftsz, fontstyle.bold),
new system.drawing.solidbrush(color.black),0,0);
//-----------------------边框
pen blackpen = new pen(color.black, 1);
ogpc.drawline(blackpen, 0, ccheight, 0, 0); // 左竖线
ogpc.drawline(blackpen, 0,0,ccwidth,0); // 顶横线
ogpc.drawline(blackpen, ccwidth-1,0,ccwidth-1,20); // 右竖线
ogpc.drawline(blackpen, 0, ccheight-1, ccwidth, ccheight-1); // 底横线
writeimg(oimg);
}
}
} // end public static void general
public static string rndstr(int32 len)
{
string stemp="";
string sforrnd="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[] arnd=sforrnd.split(',');
random ornd=new random();
int32 iarlen=arnd.length;
for(int32 i=0; i<len; i++)
{
stemp+=arnd[ornd.next(0,iarlen)];
}
return stemp;
} // end public static string rndstr
//-----------------------------------end public static method
//-----------------------------------begin private static method
private static void writeimg(bitmap oimg)
{
using(system.io.memorystream ms=new system.io.memorystream())
{
oimg.save(ms,system.drawing.imaging.imageformat.png);
httpcontext.current.response.clearcontent();
httpcontext.current.response.contenttype="image/png";
httpcontext.current.response.binarywrite(ms.toarray());
}
} // end private static void writeimg
}
//---------------------------------------------------------------------end class checkcode
</script>
上一篇: C#实现多线程的同步方法实例分析
下一篇: 在ASP.NET 中实现单点登录