扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
<%@ import namespace="System"%> <%@ import namespace="System.io"%> <%@ import namespace="System.Drawing"%> <%@ import namespace="System.Drawing.Imaging"%> <script language="vb" runat="server"> Sub Page_Load(Sender as object,e as eventargs) ’RndNum是一个自定义函数 dim VNum as string=RndNum(4) Session("VNum")=VNum ValidateCode(VNum) End Sub ’生成图象验证码函数 Sub ValidateCode(VNum) Dim Img as System.Drawing.Bitmap Dim g as Graphics Dim ms as MemoryStream dim gheight as integer=Int(Len(VNum)*11.5) ’gheight为图片宽度,根据字符长度自动更改图片宽度 img=new BitMap(Gheight,20) g=Graphics.FromImage(img) g.DrawString(VNum,(New Font("Arial",10)),(New SolidBrush(color.blue)),3,3)’在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y) ms=New MemoryStream() img.Save(ms,ImageFormat.Png) Response.ClearContent() ’需要输出图象信息 要修改HTTP头 Response.ContentType="image/Png" Response.BinaryWrite(ms.ToArray()) g.Dispose() img.Dispose() Response.End() End Sub ’-------------------------------------------- ’函数名称:RndNum ’函数参数:VcodeNum--设定返回随机字符串的位数 ’函数功能:产生数字和字符混合的随机字符串 Function RndNum(VcodeNum) dim Vchar as string="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" dim VcArray() as string=split(Vchar,",") ’将字符串生成数组 dim VNum as string="" dim i as byte For i=1 to VcodeNum Randomize VNum=VNum & VcArray(Int(35*Rnd)) ’数组一般从0开始读取,所以这里为35*Rnd Next Return VNum End Function </script> |
<asp:Image id="Image1" runat="server" ImageUrl="gif.aspx" /> |
<script language="vb" Runat="Server"> Sub Page_Load(Sender as object,e as eventargs) dim VNum as string=Session("VNum") Session.Abandon() ViewState("VNum")=VNum End Sub ’下面的事件代码是用来测试验证码,可以根据需要更改 Sub btnSubmit_click(sender as object,e as eventargs) ’判断输入的验证码与所给是否相同 If txtValidateCode.text=Cstr(ViewState("VNum")) then lblShow.text="<font color=’red’>提示:验证通过</font>" Else lblShow.text="所填写的验证码与所给的不符" End If End Sub </script> <html> <body> <form runat="server"> <div align="center"> <table width="750"> <!--DWLayoutTable--> <tr> <td width="256" height="46"> </td> <td width="9"> </td> <td width="88"> </td> <td width="87"> </td> <td width="100"> </td> <td width="68"> </td> <td width="97"> </td> </tr> <tr> <td height="21"></td> <td></td> <td colspan="3" valign="top"><asp:label ID="lblShow" runat="server"></asp:label></td> <td> </td> <td> </td> </tr> <tr> <td height="14"></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td height="21"> </td> <td colspan="2" valign="middle">验证码:</td> <td valign="top"><asp:Image id="Image1" runat="server" ImageUrl="gif.aspx" /></td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td height="20"> </td> <td colspan="2" valign="top">输入验证码:</td> <td valign="top"><asp:textbox ID="txtValidateCode" runat="server" TextMode="SingleLine" /></td> <td colspan="2" valign="middle"><font color="#FF0000" size="2">*注意:区分大小写</font></td> <td> </td> </tr> <tr> <td height="25"> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td height="19"> </td> <td> </td> <td> </td> <td valign="top"><asp:button ID="btnSubmit" runat="server" Text="比较" onclick="btnSubmit_click" /></td> <td> </td> <td> </td> <td> </td> </tr> </table> </div> </form> </body> </html> |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者