asp.net easy to generate registration code (digital + uppercase and lowercase) _ Practical skills

Source: Internet
Author: User

If there is no understanding of where, please leave a message oh

Generate random code class: SigowayRandom.cs

Copy Code code as follows:

Using System;
Namespace RongYi.Model.Common
{
<summary>
Summary description of Sigowayrandom
</summary>
public class Sigowayrandom
{
#region Get the checksum code

<summary>
Get Checksum code
</summary>
<returns> parity code character array </returns>
public static string[] Getcheckcode ()
{
string[] Strcheckcode = new String[4];

System time millisecond is a random seed
int nseed = convert.toint16 (DateTime.Now.Millisecond);
Random Random = new Random (nseed);

Generates 0-9 random numbers
Strcheckcode[0] = convert.tostring (random. Next (1, 10));
Produce A-Z, a-Z random letter
STRCHECKCODE[1] = Sigowayrandom.getletter (random);
STRCHECKCODE[2] = convert.tostring (random. Next (1, 10));
STRCHECKCODE[3] = Sigowayrandom.getletter (random);

Return checksum code
return strcheckcode;
}

#endregion

#region Get letters, case-sensitive

<summary>
Get letters, case-sensitive
</summary>
<returns> Case Letters </returns>
private static string Getletter (Random Random)
{
Random number
int NChar = random. Next (65, 122);

Non-alphabetic ASCII segments
if (NChar >= && NChar <= 96)
{
NChar-= 6;
}

Return convert.tostring ((char) nChar);
}

#endregion
}
}


Draw Check Code class: SigowayDraw.cs
Copy Code code as follows:

Using System.Drawing;
Using System.Drawing.Imaging;
Using System.Web;

Namespace RongYi.Model.Common
{
<summary>
Summary description of Sigowaydraw
</summary>
public class Sigowaydraw
{
#region Construction Method

<summary>
Construction method
</summary>
Public Sigowaydraw () {}

#endregion

#region Draw Check Code

<summary>
Draw Check Code
</summary>
<returns> Check Code </returns>
public string drawstring ()
{
Set font
Font drawfont = new Font ("Arial", 10);
Creating bitmap elements
Bitmap Objbitmap = new Bitmap (50, 20);
Create a drawing object
Graphics objgraphics = Graphics.fromimage (Objbitmap);
Set Canvas background color
Objgraphics.clear (Color.White);
Get a random string
string[] strdrawstring = Sigowayrandom.getcheckcode ();

Draw a String
Objgraphics.drawstring (Strdrawstring[0], Drawfont, New SolidBrush (Color.purple), 1, 2);
Objgraphics.drawstring (Strdrawstring[1], Drawfont, New SolidBrush (Color.green), 12, 2);
Objgraphics.drawstring (strdrawstring[2], Drawfont, New SolidBrush (color.red), 24, 2);
Objgraphics.drawstring (Strdrawstring[3], Drawfont, New SolidBrush (Color.steelblue), 35, 2);

Draw Interference Line
Objgraphics.drawline (Pens.silver, 5, 10, 40, 3);
Objgraphics.drawline (Pens.gray, 10, 5, 45, 15);
Objgraphics.drawline (Pens.hotpink, 15, 20, 30, 10);
Objgraphics.drawline (Pens.lightpink, 10, 15, 35, 20);

Draw the image into place in the drawing object
Objgraphics.drawimage (objbitmap, 0, 0);

Set save picture path and name
String strfile = HttpRuntime.AppDomainAppPath.ToString () + "/resource/img/checkcode.gif";

Output file
Objbitmap.save (strfile, imageformat.gif);

Connection Check Code string
String Strcheckcode = String. Empty;
foreach (String strtemp in strdrawstring)
{
Strcheckcode + = strtemp;
}

Return checksum code
return strcheckcode;
}

#endregion
}
}

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.