Implement a combination of numeric and character story code (C # code) under ASP.net

Source: Internet
Author: User
Tags numeric modify tostring
Asp.net| verification code to facilitate everyone to learn, modify vb.net code into C #
Contents of gif.aspx File modification
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
Rndnum is a custom function
String vnum = Rndnum (5);
session["Vnum"] = Vnum;
Validatecode (Vnum);

}
private void Validatecode (String vnum)
{
Generate validation Code
System.Drawing.Bitmap img;
System.Drawing.Graphics G;
System.IO.MemoryStream MS;
int gheight = Convert.ToInt32 (vnum.length*11.5);
Gheight for picture width, automatically change picture width based on character length
img = new Bitmap (gheight,20);
g = Graphics.fromimage (IMG);
Draws a string within a rectangle (string, font, brush color, upper left X. Upper-Left Y)
g.DrawString (vnum,new Font ("Arial"), New SolidBrush (System.Drawing.Color.Blue), 3, 3);
ms = new MemoryStream ();
Img. Save (ms,imageformat.png);

Response.clearcontent (); Need to output image information to modify HTTP headers
Response.ContentType = "Image/gif";
Response.BinaryWrite (Ms. ToArray ());
G.dispose ();
Img. Dispose ();
Response.End ();
}

' Function name: rndnum
' function parameter: vcodenum--sets the number of digits to return a random string
' function function: a random string that produces a mixture of numbers and characters
private string rndnum (int vcodenum)
{
String Vchar = "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";
string[] Vcarray = Vchar.split (",". ToCharArray ()); To generate an array of strings
String vnum = "";
int i;
for (i=1;i<vcodenum;i++)
{
System.Random ro = new Random ();
Double DecA = ro. Nextdouble ();
Vnum = Vnum + Vcarray[convert.toint32 (35*deca)];
}
For I=1 to Vcodenum
Randomize
Vnum=vnum & Vcarray (Int (35*rnd)) ' arrays are generally read from 0, so here's the 35*rnd
Next
return vnum;

}

Validatecode.aspx Modify Content
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
String Vnum;
if (session["Vnum"]!= null)
{
Vnum = session["Vnum"]. ToString ();
Session.Abandon ();
viewstate["Vnum"]=vnum;

}


}

private void Button1_Click (object sender, System.EventArgs e)
{
if (Txtvalidatecode.text = = viewstate["Vnum"). ToString ())
{

lblshow.text= "Hint: Verify through";
}
Else
{
Lblshow.text= "The verification code completed is not in conformity with the given";
}
}


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.