An ASP. NET course is provided to students. Some students asked me how to generate a verification code on the webpage?
I searched the internet and found a lot of web pages, but the code was all the same. I wrote them in C #, because the students only learned VB. net, so I changed C # To form VB. net code:
I use vs. net2005!
First, create a website in vs. net2005.
Add a class named class1.vb with the following content:
Imports Microsoft. VisualBasic
Imports system. Drawing
Public class createimage
Public shared sub drawimage ()
Dim IMG as new createimage ()
Httpcontext. Current. Session ("checkcode") = IMG. rndnum (4)
IMG. createimages (httpcontext. Current. Session ("checkcode"). tostring ())
End sub 'drawimage
'/<Summary>
'/Generate verification Image
'/</Summary>
'/<Param name = "checkcode"> Verification character </param>
Private sub createimages (byval checkcode as string)
Dim iwidth as integer = CINT (checkcode. length * 13)
Dim image as new system. Drawing. Bitmap (iwidth, 23)
Dim G as graphics = graphics. fromimage (image)
G. Clear (color. White)
'Define the color
Dim C as color () = {color. Black, color. Red, color. darkblue, color. Green, color. Orange, color. Brown, color. darkcyan, color. Purple}
'Define font
Dim font as string () = {"verdana", "Microsoft sans serif", "Comic Sans MS", "Arial", ""}
Dim rand as new random ()
'Random output noise
Dim I as integer
For I = 0 to 49
Dim X as integer = Rand. Next (image. width)
Dim y as integer = Rand. Next (image. Height)
G. drawrectangle (new pen (color. lightgray, 0), X, Y, 1, 1)
Next I
'Output Verification Code characters of different fonts and colors
For I = 0 to checkcode. Length-1
Dim cIndex as integer = Rand. Next (7)
Dim findex as integer = Rand. Next (5)
Dim F = new system. Drawing. Font (font (findex), 10, system. Drawing. fontstyle. Bold)
Dim B = new system. Drawing. solidbrush (C (cIndex ))
Dim II as integer = 4
If (I + 1) mod 2 = 0 then
II = 2
End if
G. drawstring (checkcode. substring (I, 1), F, B, 3 + I * 12, ii)
Next I
'Draw a border
G. drawrectangle (new pen (color. Black, 0), 0, 0, image. Width-1, image. Height-1)
'Output to the browser
Dim MS as new system. Io. memorystream ()
Image. Save (MS, system. Drawing. imaging. imageformat. JPEG)
Httpcontext. Current. response. clearcontent ()
'Response. clearcontent ();
Httpcontext. Current. response. contenttype = "image/JPEG"
Httpcontext. Current. response. binarywrite (Ms. toarray ())
G. Dispose ()
Image. Dispose ()
End sub 'createimages
'/<Summary>
'/Generate random letters
'/</Summary>
'/<Param name = "vcodenum"> Number of Generated letters </param>
'/<Returns> string </returns>
Private function rndnum (byval vcodenum as integer) as string
Dim allchar as string = "A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, r, S, T, U, V, W, X, Y, Z, 0, 1, 2, 4, 5, 6, 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 allchararray () as string = allchar. Split (",")
Dim randomcode as string = ""
Dim temp as integer =-1
'Record the last random value and try to avoid producing several identical random numbers.
Dim rand as random = new random
Dim I as integer = 0
Do While (I <vcodenum)
If (temp <>-1) then
Dim key as integer = ctype (datetime. Now. ticks mod system. int32.maxvalue, integer)
'Use system time to generate random Seed
Rand = new random (key)
End if
Dim t as integer = Rand. Next (allchararray. Length) + 1
If T> allchararray. Length-1 then
T = allchararray. Length-1
End if
If temp = t then
I-= 1
Randomcode = Microsoft. VisualBasic. Left (randomcode, I)
End if
Temp = T
Randomcode = randomcode + allchararray (t)
I + = 1
Loop
Return randomcode
End function 'rndnum
End Class 'createimage
Add a webpage named default2.aspx. The Code is as follows:
<% @ Page Language = "VB" autoeventwireup = "false" codefile = "default2.aspx. VB" inherits = "default2" %>
<! Doctype HTML public "-// W 3c // Dtd xhtml 1.0 transitional // en "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Verification Code Generation </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Script language = JavaScript type = "text/JavaScript">
<! __
VaR numkey = math. Random ();
Numkey = math. Round (numkey * 10000 );
Document. Write ("
// __>
</SCRIPT>
</Div>
</Form>
</Body>
</Html>
The code file name after the webpage is default2.aspx. VB. The Code is as follows:
Imports createimage
Partial class default2
Inherits system. Web. UI. Page
Protected sub page_load (byval sender as object, byval e as system. eventargs) handles me. Load
Createimage. drawimage ()
End sub
End Class
The running effect is as follows: