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
}
}