Reference code for implementing the verification code in C #

Source: Internet
Author: User

Verification Code Generation Method

Using System; using System. collections; using System. configuration; using System. data; using System. linq; using System. web; using System. web. security; using System. web. UI; using System. drawing; using System. drawing. imaging; using System. IO; using System. web. UI. htmlControls; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. xml. linq; public partial class YanZhengMa: System. we B. UI. page {protected void Page_Load (object sender, EventArgs e) {// call the RndNum function to generate a four-digit verification code string str_ValidateCode = RndNum (4 ); // save it in the Session used for verification Session ["imageCode"] = str_ValidateCode; // call the CreateImage function and draw the verification code to the image CreateImage (str_ValidateCode );} private string RndNum (int VcodeNum) {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 "; string [] V CArray = Vchar. split (new Char [] {','}); string VNum = ""; // int temp =-1; Random rand = new Random (); for (int I = 1; I <VcodeNum + 1; I ++) {int t = rand. next (36); VNum + = VcArray [t];} return VNum;} // generate Random Color private Color GetRandomColor () {Random RandomNum_First = new Random (int) DateTime. now. ticks); // There is nothing to say about the random number of C. threading. thread. sleep (RandomNum_First.Next (50); Random RandomN Um_Sencond = new Random (int) DateTime. now. ticks); // generate a dark int int_Red = RandomNum_First.Next (256); int int_Green = RandomNum_Sencond.Next (256 ); int int_Blue = (int_Red + int_Green> 400 )? 0: 400-int_Red-int_Green; int_Blue = (int_Blue> 255 )? 255: int_Blue; return Color. fromArgb (int_Red, int_Green, int_Blue);} public void CreateImage (string str_ValidateCode) {int int_ImageWidth = str_ValidateCode.Length * 13; Random newRandom = new Random (); // figure height: 20px Bitmap theBitmap = new Bitmap (int_ImageWidth, 20); Graphics theGraphics = Graphics. fromImage (theBitmap); // theGraphics with a white background. clear (Color. white); // The gray border theGraphics. drawRectangle (new Pen (Color. lightGray, 1), 0, 0, int_ImageWidth-1, 19); // Font of 10pt theFont = new Font ("Arial", 10 ); for (int int_index = 0; int_index <str_ValidateCode.Length; int_index ++) {string str_char = str_ValidateCode.Substring (int_index, 1); Brush newBrush = new SolidBrush (GetRandomColor ()); point thePos = new Point (int_index * 13 + 1 + newRandom. next (3), 1 + newRandom. next (3); theGraphics. drawString (str_char, theFont, newBrush, thePos);} // send the generated image back to the client MemoryStream MS = new MemoryStream (); theBitmap. save (MS, ImageFormat. jpeg); Response. clearContent (); // You Need to output the image information to modify the HTTP header Response. contentType = "image/Png"; Response. binaryWrite (ms. toArray (); theGraphics. dispose (); theBitmap. dispose (); Response. end ();}}

Part of the Code called at the front-end

View Code

1 <% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" Inherits = "CRM. _ Default" %>
2
3 <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 6 7 <title> No title page </title>
8 <script type = "text/javascript">
9 function UpdateImage ()
10 {
11 var n = Math. random ();
12 document. getElementById ("image"). src = "System/CheckCode. aspx? R = "+ n;
13}
14 </script>
15
16 <body>
17 <form id = "form1" runat = "server">
18 <div>
19
20
21
22 <a href = "javascript: UpdateImage ()"> I cannot see it clearly. Change it to another one. </a>
23
24 </div>
25 </form>
26 </body>
27

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.