*如果你觉得本人的文章好,要引用请尊重著作人的劳动果实,说明
*出处以及原创作者,thank you!!! email:aishen944-sohu.com
*******************************************************************/
using system;
using system.drawing;
using system.drawing.imaging;
using system.web.ui;
using system.drawing.drawing2d;
using system.io;
namespace validate
{
/// <summary>
/// 生成验证码的类
/// </summary>
public class validatenumber
{
public validatenumber()
{
}
/// <summary>
/// 验证码的最大长度
/// </summary>
public int maxlength
{
get{return 10;}
}
/// <summary>
/// 验证码的最小长度
/// </summary>
public int minlength
{
get{return 1;}
}
/// <summary>
/// 生成验证码
/// </summary>
/// <param name="length">指定验证码的长度</param>
/// <returns></returns>
public string createvalidatenumber(int length)
{
int[] randmembers=new int[length];
int[] validatenums=new int[length];
string validatenumberstr="";
//生成起始序列值
int seekseek=unchecked((int)datetime.now.ticks);
random seekrand=new random(seekseek);
int beginseek=(int)seekrand.next(0,int32.maxvalue-length*10000);
int[] seeks=new int[length];
for(int i=0;i<length;i++)
{
beginseek+=10000;
seeks[i]=beginseek;
}
//生成随机数字
for(int i=0;i<length;i++)
{
random rand=new random(seeks[i]);
int pownum=1*(int)math.pow(10,length);
randmembers[i]=rand.next(pownum,int32.maxvalue);
}
//抽取随机数字
for(int i=0;i<length;i++)
{
string numstr=randmembers[i].tostring();
int numlength=numstr.length;
random rand=new random();
int numposition=rand.next(0,numlength-1);
validatenums[i]=int32.parse(numstr.substring(numposition,1));
}
//生成验证码
for(int i=0;i<length;i++)
{
validatenumberstr+=validatenums[i].tostring();
}
return validatenumberstr;
}
/// <summary>
/// 创建验证码的图片
/// </summary>
/// <param name="containspage">要输出到的page对象</param>
/// <param name="validatenum">验证码</param>
public void createvalidategraphic(page containspage,string validatenum)
{
bitmap image=new bitmap((int)math.ceiling(validatenum.length*12.5),22);
graphics g=graphics.fromimage(image);
try
{
//生成随机生成器
random random=new random();
//清空图片背景色
g.clear(color.white);
//画图片的干扰线
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 font("arial",12,(fontstyle.bold|fontstyle.italic));
lineargradientbrush brush=new lineargradientbrush(new rectangle(0,0,image.width,image.height),
color.blue,color.darkred,1.2f,true);
g.drawstring(validatenum,font,brush,3,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);
//保存图片数据
memorystream stream=new memorystream();
image.save(stream,imageformat.jpeg);
//输出图片
containspage.response.clear();
containspage.response.contenttype="image/jpeg";
containspage.response.binarywrite(stream.toarray());
}
finally
{
g.dispose();
image.dispose();
}
}
/// <summary>
/// 得到验证码图片的长度
/// </summary>
/// <param name="validatenumlength">验证码的长度</param>
/// <returns></returns>
public static int getimagewidth(int validatenumlength)
{
return (int)(validatenumlength*12.5);
}
/// <summary>
/// 得到验证码的高度
/// </summary>
/// <returns></returns>
public static double getimageheight()
{
return 22.5;
}
}
}
商业源码热门下载www.html.org.cn
新闻热点
疑难解答
图片精选