ASP. NET (VB) image Verification Code

Source: Internet
Author: User

According to the Netizen's C #. Net image verification, it was changed to VB. NET. Because VB. NET does not support unchecked, the random number generated by time seed is removed.

Number. The same verification code is not displayed during the test.

Validcode. VB
--------------
Imports system
Imports system. Drawing
Imports system. Web
Imports system. Web. UI

Public class validcode
Inherits page

'Input verification Image
Public sub drawimage ()
Dim thecode as string
Thecode = rndnum (4)
Httpcontext. Current. Session ("validcode") = thecode' Save the verification code to the session for verification.
Createvalidimage (thecode)
End sub

'Generate verification Image
'Parameter: checkcode verification character
Private sub createvalidimage (checkcode as string)
Dim iwidth as integer = checkcode. length * 15
Dim image as new Bitmap (iwidth, 25) 'image size
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. Red,

Color. Brown, color. darkcyan, color. Purple}

'Define font
Dim font as string () = {"verdana", "system", "Comic Sans MS", "Arial", ""}

'Random output noise
Dim rand as new random ()
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

'Output Verification Code characters of different fonts and colors
Dim J as short
For J = 0 to checkcode. Length-1
Dim cIndex as short = Rand. Next (8) 'random color
Dim findex as short = Rand. Next (5) 'random font
Dim F as new font (findex), 12, fontstyle. Regular) '12 lbs, italic
Dim B as new solidbrush (C (cIndex) 'defines a monochrome paint brush

Dim pointy as integer = 4
If (J + 1) mod 2 = 0 then
Pointy = 2
End if
G. drawstring (checkcode. substring (J, 1), F, B, 3 + (J * 13), pointy)
Next

'Draw an External Frame
G. drawrectangle (new pen (color. Gray, 0), 0, 0, image. Width-1, image. Height-1)

'Output to the browser
Dim msoutput as new system. Io. memorystream ()
Image. Save (msoutput, imaging. imageformat. JPEG)
Httpcontext. Current. response. clearcontent ()
Httpcontext. Current. response. contenttype = "image/JPEG"
Httpcontext. Current. response. binarywrite (msoutput. toarray ())

'Release
G. Dispose ()
Image. Dispose ()
End sub

'Generate a random combination of letters or numbers
'Parameter: number of random letters or numbers generated by vcodenum
Private function rndnum (vcodenum as short) as string
Dim vchar as string
Vchar = ,"&_
"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 ,"&_
"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"
Dim vchararray as string () = vchar. Split (",")
Dim arraylength as integer = vchararray. Length 'array Length
Dim rand as new random ()
Dim rndint as integer 'random number
Dim codenum as string = "" 'is used to return results
 
Dim I as integer
For I = 0 to vcodenum-1
Rndint = Rand. Next (arraylength)
Codenum & = vchararray (rndint)
Next

Return codenum
End Function

End Class
------------

Imagecode. aspx
<% @ Page Language = "VB" inherits = "validcode" src = "validcode. VB" %>
<SCRIPT runat = "server">
Sub page_load ()
Dim imgcode as new validcode
Imgcode. drawimage ()
End sub
</SCRIPT>
------------

Login. aspx
<Script language = "VB" runat = "server">
Sub changecode_click (S as object, e as eventargs)
Imagecode. databind ()
End sub
</SCRIPT>
<Body> <form runat = "server">
<P> <asp: textbox id = "code" runat = "server"/>
<Asp: Image id = "imagecode" imageurl = "imagecode. aspx" runat = "server"/>
<P> <asp: linkbutton id = "changecode" text = "cannot see clearly? Change the verification code "onclick =" changecode_click"

Runat = "server"/>
</Form>
</Body>
-----------

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1465934

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.