Collect a small section of the. NET code snippet below

Source: Internet
Author: User
Verification Code

Collect a small section of the. NET validation code snippet for later reference.

 protected void Page_Load (object sender, EventArgs e)
    {
         //Generate numeric string first
         string checkcode = this. Createrandomcode (6);
       //Save with session
         session["Checkcode"] = Checkcode;
     //Drawing
        createimage (checkcode);
       
   }
    private void CreateImage ( String checkcode)
    {
        System.Drawing.Bitmap Image = New System.Drawing.Bitmap (Convert.ToInt32 (math.ceiling (decimal) (Checkcode.length * 14)), 22);
        Graphics g = graphics.fromimage (image);
       

Try
{

Random Random = new Random ();
G.clear (Color.aliceblue);

for (int i = 0; i < 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 System.Drawing.Font ("Comic Sans MS ", System.Drawing.FontStyle.Bold);
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush (New Rectangle (0, 0, image.) Width, image. Height), Color.Blue, color.darkred, 1.2f, true);
            g.drawstring (checkcode, Font, new SolidBrush (Color.Red), 2, 2);


for (int i = 0; i < i++)
{
int x = random. Next (image. Width);
int y = random. Next (image. Height);

Image. SetPixel (x, Y, Color.FromArgb) (random. Next ()));
}

G.drawrectangle (New Pen (Color.silver), 0, 0, image. Width-1, image. HEIGHT-1);

            System.IO.MemoryStream ms = new System.IO.MemoryStream ();
            image. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF);
            response.clearcontent ();
            Response.ContentType = "Image/gif";
            Response.BinaryWrite (Ms. ToArray ());
       }
        finally
        {
             G.dispose ();
            image. Dispose ();
       }
   }

public string Createrandomcode (int codecount)
{
String Allchar = "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,v,w,x,y,z";
string[] Allchararray = Allchar.split (', ');
String randomcode = "";
int temp =-1;

Random rand = new Random ();
for (int i = 0; i < Codecount; i++)
{
if (temp!=-1)
{
Rand = new Random (i * temp * ((int) DateTime.Now.Ticks));
}
int t = rand. Next (36);
if (temp!=-1 && temp = t)
{
Return Createrandomcode (Codecount);
}
temp = t;
Randomcode + = allchararray[t];
}
return randomcode;
}



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.