VS2005中验证码生成页


<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="code.aspx.cs"Inherits="code"%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>鏃犳爣棰橀〉</title>
</head>
<body>
<formid="form1"runat="server">
<div>

</div>
</form>
</body>
</html>

usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Drawing.Imaging;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;


///<summary>
///code验证码生成页
///</summary>

publicpartialclasscode:System.Web.UI.Page
{
//验证码长度
privateintcodeLen=8;
//图片清晰度
privateintfineness=80;
//图片宽度
privateintimgWidth=128;
//图片高度
privateintimgHeight=24;
//字体家族名称
privatestringfontFamily="TimesNewRoman";
//字体大小
privateintfontSize=14;
//字体样式
privateintfontStyle=0;
//绘制起始坐标X
privateintposX=0;
//绘制起始坐标Y
privateintposY=0;

//------------------------------------------------------------
//code.aspx页面加载函数
//------------------------------------------------------------
privatevoidPage_Load(objectsender,System.EventArgse)
{
读取Request传递参数

stringcode=Createcode();

//生成BITMAP图像
Bitmapbitmap=newBitmap(imgWidth,imgHeight);

//给图像设置干扰
DisturbBitmap(bitmap);

//绘制验证码图像
Drawcode(bitmap,code);

//保存验证码图像,等待输出
bitmap.Save(Response.OutputStream,ImageFormat.Gif);
}



//------------------------------------------------------------
//随机生成验证码,并保存到SESSION中
//------------------------------------------------------------
privatestringCreatecode()
{
stringcode="";

//随机数对象
Randomrandom=newRandom();

for(inti=0;i<codeLen;i++)
{
//26:a-z
intn=random.Next(26);

//将数字转换成大写字母
code+=(char)(n+65);
}


//保存验证码
Session["code"]=code;

returncode;
}


//------------------------------------------------------------
//为图片设置干扰点
//------------------------------------------------------------
privatevoidDisturbBitmap(Bitmapbitmap)
{
//通过随机数生成
Randomrandom=newRandom();

for(inti=0;i<bitmap.Width;i++)
{
for(intj=0;j<bitmap.Height;j++)
{
if(random.Next(100)<=this.fineness)
bitmap.SetPixel(i,j,Color.White);
}

}


}


//------------------------------------------------------------
//绘制验证码图片
//------------------------------------------------------------
privatevoidDrawcode(Bitmapbitmap,stringcode)
{
//获取绘制器对象
Graphicsg=Graphics.FromImage(bitmap);

//设置绘制字体
Fontfont=newFont(fontFamily,fontSize,GetFontStyle());
PenmyPen
=newPen(Color.Black,2);

//随机画两条线
Randomr=newRandom();
floatx1=r.Next(imgWidth);
floatx2=r.Next(imgWidth);
floaty1=r.Next(imgHeight);
floaty2=r.Next(imgHeight);
g.DrawLine(myPen,x1,y1,x2,y2);
x1
=r.Next(imgWidth);
x2
=r.Next(imgWidth);
y1
=r.Next(imgHeight);
y2
=r.Next(imgHeight);
g.DrawLine(myPen,x1,y1,x2,y2);
//绘制验证码图像
g.DrawString(code,font,Brushes.Black,posX,posY);

}


//------------------------------------------------------------
//换算验证码字体样式:1粗体2斜体3粗斜体,默认为普通字体
//------------------------------------------------------------
privateFontStyleGetFontStyle()
{
if(fontStyle==1)
returnFontStyle.Bold;
elseif(fontStyle==2)
returnFontStyle.Italic;
elseif(fontStyle==3)
returnFontStyle.Bold|FontStyle.Italic;
else
returnFontStyle.Regular;
}

}



优质内容筛选与推荐>>
1、类与对象 动手动脑
2、something about the SV_POSITION
3、iOS image processing with the accelerate framework(透明玻璃效果)
4、ThinkPHP CURD方法盘点:data方法
5、tf.pad 用法说明


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn