asp.net validation code generation Class (reference)

Source: Internet
Author: User
Tags rand reference
Asp.net| Reference | Verification Code **********************created by chen**************************

* If you feel that my article is good, to quote please respect the work of the authors of the fruits of labor, explain

* Source and original creator, Thank you!!! Email:aishen944-sohu.com

*******************************************************************/
Using System;
Using System.Drawing;
Using System.Drawing.Imaging;
Using System.Web.UI;
Using System.Drawing.Drawing2D;
Using System.IO;
Namespace Validate
{
<summary>
Class that generates the verification code
</summary>
public class ValidateNumber
{
Public ValidateNumber ()
{
}
<summary>
Maximum length of verification code
</summary>
public int MaxLength
{
Get{return 10;}
}
<summary>
Minimum length of verification code
</summary>
public int MinLength
{
Get{return 1;}
}
<summary>
Generate Verification Code
</summary>
<param name= "Length" > Specified verification code lengths </param>
<returns></returns>
public string createvalidatenumber (int length)
{
Int[] Randmembers=new int[length];
Int[] Validatenums=new int[length];
String Validatenumberstr= "";
Generate Start Sequence values
int seekseek=unchecked ((int) DateTime.Now.Ticks);
Random seekrand=new Random (Seekseek);
int beginseek= (int) seekrand.next (0,int32.maxvalue-length*10000);
Int[] Seeks=new int[length];
for (int i=0;i<length;i++)
{
beginseek+=10000;
Seeks[i]=beginseek;
}
Generate random numbers
for (int i=0;i<length;i++)
{
Random rand=new Random (Seeks[i]);
int pownum=1* (int) Math.pow (10,length);
Randmembers[i]=rand. Next (Pownum,int32.maxvalue);
}
Extract random numbers
for (int i=0;i<length;i++)
{
String Numstr=randmembers[i]. ToString ();
int numlength=numstr.length;
Random rand=new Random ();
int Numposition=rand. Next (0,numlength-1);
Validatenums[i]=int32.parse (numstr.substring (numposition,1));
}
Generate Verification Code
for (int i=0;i<length;i++)
{
Validatenumberstr+=validatenums[i]. ToString ();
}
return validatenumberstr;
}
<summary>
Create a picture of the verification code
</summary>
<param name= "Containspage" > Page object to Output </param>
<param name= "Validatenum" > Verification Code </param>
public void Createvalidategraphic (Page containspage,string validatenum)
{
Bitmap image=new Bitmap ((int) math.ceiling (validatenum.length*12.5), 22);
Graphics g=graphics.fromimage (image);
Try
{
Generate a random generator
Random random=new Random ();
Empty picture background color
G.clear (Color.White);
Disturbing lines for drawing pictures
for (int i=0;i<25;i++)
{
int X1=random. Next (image. Width);
int X2=random. Next (image. Width);
int Y1=random. Next (image. Height);
int Y2=random. Next (image. Height);
G.drawline (New Pen (Color.silver), x1,y1,x2,y2);
}
Font font=new font ("Arial", fontstyle.bold| Fontstyle.italic));
LinearGradientBrush brush=new LinearGradientBrush (New Rectangle (0,0,image. Width,image. Height),
Color.blue,color.darkred,1.2f,true);
g.DrawString (validatenum,font,brush,3,2);
Picture of the foreground interference point
for (int i=0;i<100;i++)
{
int X=random. Next (image. Width);
int Y=random. Next (image. Height);
Image. SetPixel (X,Y,COLOR.FROMARGB) (random. Next ()));
}
Draw a picture's border line
G.drawrectangle (New Pen (Color.silver), 0,0,image. Width-1,image. HEIGHT-1);
Save picture data
MemoryStream stream=new MemoryStream ();
Image. Save (Stream,imageformat.jpeg);
Output picture
ContainsPage.Response.Clear ();
Containspage.response.contenttype= "Image/jpeg";
ContainsPage.Response.BinaryWrite (stream. ToArray ());
}
Finally
{
G.dispose ();
Image. Dispose ();
}
}
<summary>
Get the length of the Verification code picture
</summary>
<param name= "Validatenumlength" > Verification Code length </param>
<returns></returns>
public static int getimagewidth (int validatenumlength)
{
return (int) (validatenumlength*12.5);
}
<summary>
Get the height of the verification code
</summary>
<returns></returns>
public static double Getimageheight ()
{
return 22.5;
}
}
}




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.