| ''' <summary> ''' 产生随机数(包含字母与数字)用于校验码 ''' </summary> ''' <param name="CodeLength"></param> ''' <returns></returns> ''' <remarks></remarks> Private Function generateVCode(ByVal CodeLength As Integer) As String Dim VCode As String = String.Empty Dim randObj As New Random() Dim c As Integer = 63 For i As Byte = 1 To CodeLength c = randObj.Next(35) If c >= 10 Then c += 7 End If c += 48 VCode += Chr(c) Next Return VCode End Function |
| ''' <summary> ''' 产生随机的笔触样式(用于图像的背景) ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Private Function generateHatchStyle() As HatchStyle Dim slist As New ArrayList For Each style As HatchStyle In System.Enum.GetValues(GetType(HatchStyle)) slist.Add(style) Next Dim randObj As New Random() Dim index As Integer = randObj.Next(slist.Count - 1) Return CType(slist(index), HatchStyle) End Function |
| ''' <summary> ''' 产生随机数校验码图像 ''' </summary> ''' <remarks></remarks> Private Function GenerateVCodeImage() Dim oBitmap As Bitmap = New Bitmap(90, 35) Dim oGraphic As Graphics = Graphics.FromImage(oBitmap) Dim foreColor As System.Drawing.Color Dim backColor As System.Drawing.Color Dim sText As String = generateVCode(5) '获取校验码字符串 Dim sFont As String = "Comic Sans MS" '设置自己喜欢的字体 '前景、背景的颜色 foreColor = Color.FromArgb(220, 220, 220) backColor = Color.FromArgb(190, 190, 190) '设置用于背景的画笔 Dim oBrush As New HatchBrush(CType(generateHatchStyle(), HatchStyle), foreColor, backColor) '用于输出校验码的画笔 Dim oBrushWrite As New SolidBrush(Color.Gray) '生成的图像矩形大小 oGraphic.FillRectangle(oBrush, 0, 0, 100, 50) oGraphic.TextRenderingHint = TextRenderingHint.AntiAlias Dim oFont As New Font(sFont, 14) Dim oPoint As New PointF(5.0F, 4.0F) oGraphic.DrawString(sText, oFont, oBrushWrite, oPoint) Response.ContentType = "image/jpeg" oBitmap.Save(Response.OutputStream, ImageFormat.Jpeg) oBitmap.Dispose() Return sText End Function |
| Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim code As String = GenerateVCodeImage() Session("VCode") = code End Sub |
![]() |
| <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="btnCheck" runat="server" Text="Check It" /> <asp:Label ID="lblMessage" runat="server"></asp:Label><br /> <br /> <asp:Image ID="Image1" runat="server" Height="32px" Width="104px" ImageUrl='VCode.aspx' /> <br /> <br /> |
| Protected Sub btnCheck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCheck.Click Dim code As String = Session("VCODE") If TextBox1.Text.Trim.ToUpper = code Then lblMessage.Text = "校验成功!" lblMessage.ForeColor = Color.Blue Else lblMessage.Text = "您输入的注册码错误!" lblMessage.ForeColor = Color.Red End If TextBox1.Text = "" End Sub |
![]() |
| Private Function generateVCode(ByVal CodeLength As Integer) As String Dim VCode As String = String.Empty Dim randObj As New Random() Dim c As Integer = 63 For i As Byte = 1 To CodeLength '是否显示 数字:0, 因为数字0 与字母O 容易混淆 'While (c = 63) ' c = randObj.Next(35) 'End While c = randObj.Next(35) If c >= 10 Then c += 7 End If c += 48 VCode += Chr(c) Next Return VCode End Function |
文档说明:
相关文档
返回首页 | 关于本站 | | 友情链接 | 广告服务 | 意见建议 | 访客留言 | 本站论坛
Copyright© 2001-2006 ProgramBBS.com All Rights Reserved 版权所有©编程论坛
Email: 吉ICP备05009985号
感谢长春订餐网友情支持