I often see how to ask how to implement the verification code, in fact, there are many articles on the verification code in the 9CBS, DEV-Club, etc., but many articles only spend how to output a random generated number or character. The image, of course, this is the core of the verification code, but for many ASP.NET's beginners, how to use the image it generated again (forum has a lot of ask this), this is also I wrote a reason for this article.
For the reason, I will not say much about the principle of verification code. You can see other articles. The end of the article is accompanied by a complete example code. There is a detailed note, you can skip the explanation of words, use directly
First, I want to briefly talk about the usage of session and viewstate, because it will be used later.
Store data in session: Session ("key") = "test"
Take the value from the session: DIM TestValue As String = Session ("Key")
akin:
Store data in ViewState: ViewState ("Key") = "TEST"
Take the value from ViewState: DIM TestValue As String = ViewState ("Key")
About ViewState's more detailed information, you can see the "ASP.NET ViewState for MSDN"
It's better to see it, sometimes the code itself is more expressive than any explanation, so there is no more code to explain too much. The verification code implemented by this article needs to use two files:
GIF.ASPX This file is used to generate a verification code
Validatecode.aspx This file is used to test the verification code (ie how to use)
Here is the full code of GIF.ASPX:
<% @ Import namespace = "system"%>
<% @ Import namespace = "system.io"%>
<% @ Import namespace = "system.drawing"%>
<% @ Import namespace = "system.drawing.imaging"%>
SUB Page_Load (Sender As Object, E as Eventargs)
'RNDNUM is a custom function
Dim vnum as string = rNDNUM (4)
Session ("VNUM") = VNUM
Validatecode (VNUM)
End Sub
'Generate image verification code functions
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 is the width, automatically change image width according to character length
IMG = New Bitmap (Gheight, 20)
g = graphics.fromImage (IMG)
g.drawstring (New Font ("Arial", 10)), (New SolidBrush), 3, 3) 'Draw strings in the rectangular shape (string, font, brush color, left X X . Top y)
MS = new memoryStream ()
Img.save (ms, imageformat.png)
Response.clearContent () 'Requires Output Image Information To modify http headers
Response.contentType = "image / png" response.binarywrite (ms.toarray ())
g.dispose ()
IMG.Dispose ()
Response.end ()
End Sub
'--------------------------------------------
'Function name: rwnum
'Function parameters: vcodenum - Set the number of bits that return the random string
'Function function: generate random strings that generate numbers and characters
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, ",") 'Generates arrays in strings
DIM vnum as string = "" "
DIM I as byte
For i = 1 to vcodenum
Randomize
Vnum = vnum & vcarray (INT (35 * RND)) 'The array is generally read from 0, so here is 35 * RND
NEXT
Return Vnum
END FUNCTION
script>
So how should I use the image verification code generated by this file, see this code:
This is the image control used to display the verification code. You can put it in any where you like, the following gives a detailed usage code, you save it as validatecode.aspx, and put it and gif.aspx Under the same directory, open validatecode.aspx in your browser, you can test it:
SUB Page_Load (Sender As Object, E as Eventargs)
DIM VNUM AS STRING = session ("vnum")
Session.abandon ()
ViewState ("vnum") = vnum
End Sub
'The following event code is used to test the verification code, can be changed as needed.
SUB BTNSUBMIT_CLICK (Sender As Object, E AS Eventargs)
'Judgment the input verification code and whether the same is the same
IF txtvalidatecode.text = cstr (ViewState ("vnum")) THEN
LBLSHOW.TEXT = " Tip: Verify via font>"
Else
LBLSHOW.TEXT = "The verification code filled in and does not match it"
END IF
End Sub
script>