asp.net simple method of generating verification code _ practical skills

Source: Internet
Author: User

This article illustrates the method of asp.net simple generation of verification code. Share to everyone for your reference, specific as follows:

1. Create a new generic handler

namespace WebApplication1 {///<summary>///$codebehindclassname $ summary Description///</summary> [WebService (Name Space = "http://tempuri.org/")] [webservicebinding (ConformsTo = wsiprofiles.basicprofile1_1)] public class Handler1:ih Ttphandler, irequiressessionstate {public void ProcessRequest (HttpContext context) {context.
   Response.ContentType = "Image/gif";
   Set up Bitmap object, draw Bitmap Basemap = new Bitmap (100, 30);
   Graphics graph = graphics.fromimage (basemap); Graph.
   FillRectangle (New SolidBrush (Color.White), 0, 0, 100, 30);
   Font font = new Font (FONTFAMILY.GENERICSERIF, fontstyle.bold, GraphicsUnit.Pixel);
   Random r = new Random ();
   String letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
   String Letter;
   StringBuilder s = new StringBuilder (); Add a random five-letter for (int x = 0; x < 5 + x + +) {letters = letters. Substring (r.next (0, letters).
    Length-1), 1);
    S.append (letter); Graph. DrawString (letter, font, New SolidBrush (Color.Black), X * 19,R.next (0, 8));
   //confusing background pen linepen = new Pen (new SolidBrush (Color.Black), 2); for (int x = 0; x < 6 + + +) graph.
   DrawLine (Linepen, New Point (R.next (0, $), R.next (0)), New Point (R.next (0,), R.next (0, 29)); Save the picture to the output stream basemap. Save (context.
   Response.outputstream, imageformat.gif); Context. session["Checkcode"] = s.tostring (); If IRequiresSessionState is not implemented, there is an error and the picture context cannot be generated.
  Response.End ();
   public bool IsReusable {get {return false;

 }
  }
 }
}

2. Front Code

 
 

I hope this article will help you to ASP.net program design.

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.