Asp.net image Verification Code (VB.net)

Source: Internet
Author: User

Implementation process: On the page to be called, use the image control to call the page generated by the Verification Code. On the page generated by the Verification Code, generate a verification image and save the verification information to the session, the call page uses session to determine whether the user input is correct. The reason is that the following code is used:

<% @ Import namespace = "system" %>
<% @ Import namespace = "system. Io" %>
<% @ Import namespace = "system. Drawing" %>
<% @ Import namespace = "system. Drawing. Imaging" %>

<Script language = "VB" runat = "server">
Sub page_load () sub page_load (byval sender as object, byval e as eventargs)
'Rndnum is a function for generating random codes,
Dim vnum as string = rndnum (4) 'this value is the number of digits that generate the verification code
Session ("vnum") = vnum' read session
Validatecode (vnum) 'generates an image based on the session
End sub

'--------------------------------------------
'Image verification code generation function
Sub validatecode () sub validatecode (byval vnum)
Dim IMG as system. Drawing. Bitmap
Dim G as graphics
Dim MS as memorystream
Dim gheight as integer = int (LEN (vnum) * 14)
'Gheight indicates the image width. The image width is automatically changed based on the Character length.
IMG = new Bitmap (gheight, 24)
G = graphics. fromimage (IMG)
G. drawstring (vnum, (new font ("Arial", 12), (new solidbrush (color. red), 3, 3) 'draw the string (string, Font, paint color, top left X in the rectangle. Y on the top left)
MS = new memorystream ()
IMG. Save (MS, imageformat. PNG)
Response. clearcontent () 'needs to output image information to modify the HTTP Header
Response. contenttype = "image/PNG"
Response. binarywrite (Ms. toarray ())
G. Dispose ()
IMG. Dispose ()
Response. End ()
End sub

'--------------------------------------------
'Function name: rndnum
'Function parameter: vcodenum -- set the number of digits of the returned random string
'Function: generate a random string mixed with numbers and characters
Function rndnum () function rndnum (byval vcodenum)
Dim vchar as string = ", 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 "'needs to be verified in Chinese. You can modify the font here and in the validatecode function.
Dim vcarray () as string = Split (vchar, ",") 'generates an array of strings
Dim vnum as string = ""
Dim I as byte
For I = 1 to vcodenum
Randomize ()
Vnum = vnum & vcarray (INT (35 * RND () 'array is generally read from 0, so here it is 35 * RND
Next
Return vnum
End Function
</SCRIPT>

 

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.