一个.net图片验证码的实现

页面代码
1<asp:Image id="Image1" width="100" runat="server" ImageUrl="~/manage/mycheckcode.aspx"></asp:Image>
后台代码实现
 If Txt_code.Text.Trim <> Session("checkcode").ToString Then
            Alert(Page, 
"输入验证码错误,请重新输入")
            
Exit Sub
        
End If
主要实现
  1Imports System.Drawing.Imaging
  2Imports System.Drawing.Drawing2D
  3Public Class MycheckCodedd
  4    Inherits System.Web.UI.Page
  5
  6Web 窗体设计器生成的代码
 24
 25    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 26        '在此处放置初始化页的用户代码
 27        Response.Buffer = True
 28        draw2(getNum.ToString)
 29    End Sub

 30
 31    Private Function getNum() As Int64
 32        Dim zNum, i, j As Int64
 33
 34        Randomize(Timer)
 35        zNum = CInt(8999 * Rnd() + 1000)
 36        Session("CheckCode"= zNum
 37        Return zNum
 38    End Function

 39
 40    Private Sub draw()
 41        Response.ContentType = "image/jpeg"
 42        Dim width As Int32
 43        Dim height As Int32
 44        width = 300
 45        height = 300
 46        Dim x, y As Int16
 47        x = 30
 48        y = 50
 49        Dim zNum, i, j
 50        Dim Ados, Ados1
 51        Randomize(Timer)
 52        zNum = CInt(8999 * Rnd() + 1000)
 53        Session("CheckCode"= zNum
 54
 55        Dim objBitmap As Bitmap
 56        objBitmap = New Bitmap(width, height)
 57
 58        Dim objGraphics As Graphics
 59        objGraphics = Graphics.FromImage(objBitmap)
 60
 61        objGraphics.DrawRectangle(New Pen(Color.Black), 00, width, height)
 62        objGraphics.FillRectangle(New SolidBrush(Color.White), 11, width - 2, height - 2)
 63
 64        Dim objBrush As New SolidBrush(Color.Blue)
 65        objGraphics.SmoothingMode = SmoothingMode.AntiAlias
 66        objBitmap.Save(Response.OutputStream, ImageFormat.Jpeg)
 67        objGraphics.Dispose()
 68        objBitmap.Dispose()
 69    End Sub

 70
 71    Private Sub draw2(ByVal str As String)
 72        Dim image As New Bitmap(600200)
 73        Dim axesfont As New Font("arial"110, FontStyle.Bold)
 74
 75        Dim blackbrush As New SolidBrush(Color.SteelBlue)
 76        Dim redpen As New Pen(Color.Red, 5)
 77        Dim blackpen As New Pen(Color.Black, 2)
 78        Dim g As Graphics
 79
 80        g = Graphics.FromImage(image)
 81        g.Clear(Color.White)
 82        g.FillRectangle(New SolidBrush(Color.White), 00600200)
 83        g.DrawString(str, axesfont, blackbrush, 1010)
 84        ' g.DrawLine(blackpen, 0, 2, 210, 250)
 85        ' g.DrawLine(redpen, 600, 200, 0, 0)
 86        ' g.DrawLine(blackpen, 210, 250, 310, 50)
 87        ' g.DrawLine(redpen, 310, 50, 210, 350)
 88        image.Save(Response.OutputStream, ImageFormat.Jpeg)
 89
 90    End Sub

 91
 92
 93
 94    Function NumCode()
 95        Response.Expires = -1
 96        Response.AddHeader("Pragma""no-cache")
 97        Response.AddHeader("cache-ctrol""no-cache")
 98        On Error Resume Next
 99        Dim zNum, i, j
100        Dim Ados, Ados1
101        Randomize(Timer)
102        zNum = CInt(8999 * Rnd() + 1000)
103        Session("CheckCode"= zNum
104        Dim zimg(4), NStr
105        NStr = CStr(zNum)
106        For i = 0 To 3
107            zimg(i) = CInt(Mid(NStr, i + 11))
108        Next
109        Dim Pos
110        Ados = Server.CreateObject("Adodb.Stream")
111        Ados.Mode = 3
112        Ados.Type = 1
113        Ados.Open()
114        Ados1 = Server.CreateObject("Adodb.Stream")
115        Ados1.Mode = 3
116        Ados1.Type = 1
117        Ados1.Open()
118        Ados.LoadFromFile(Server.MapPath("body.Fix"))
119        Ados1.write(Ados.read(1280))
120        For i = 0 To 3
121            Ados.Position = (9 - zimg(i)) * 320
122            Ados1.Position = i * 320
123            Ados1.write(Ados.read(320))
124        Next
125        Ados.LoadFromFile(Server.MapPath("head.fix"))
126
127        Pos = System.Text.Encoding.Default.GetBytes(Ados.read()).Length()
128
129        Ados.Position = Pos
130        For i = 0 To 9 Step 1
131            For j = 0 To 3
132                Ados1.Position = i * 32 + j * 320
133                Ados.Position = Pos + 30 * j + i * 120
134                Ados.write(Ados1.read(30))
135            Next
136        Next
137        Response.ContentType = "image/BMP"
138        Ados.Position = 0
139        Response.BinaryWrite(Ados.read())
140        Ados.Close() : Ados = Nothing
141        Ados1.Close() : Ados1 = Nothing
142        If Err.Description <> "" Then Session("CheckCode"= 9999
143    End Function

144
145
146
147End Class

148

posted on 2005-11-16 21:11  黑月  阅读(1321)  评论(1编辑  收藏  举报

导航