asp.net how to generate a picture verification code (simple)

Source: Internet
Author: User

Create a new page image.aspx and add namespaces:
Using System.Drawing.Imaging;
Using System.IO;
Then copy the following code into the Page_Load event:
Generate a 4-bit verification code
string tmp = Rndnum (4);
HttpCookie a = new HttpCookie ("Imagev", TMP);
Response.Cookies.Add (a);
This. Validatecode (TMP);
Next, add two methods to the page:
private void Validatecode (String vnum)
{
Bitmap Img=null;
Graphics G=null;
MemoryStream Ms=null;
int gheight=vnum.length*12;
Img=new Bitmap (gheight,25);
G=graphics.fromimage (IMG);
Background color
G.clear (Color.White);
Text font
Font f=new font ("Arial black", 10);
Text color
SolidBrush s=new SolidBrush (color.black);
g.DrawString (vnum,f,s,3,3);
Ms=new MemoryStream ();
Img.save (Ms,imageformat.jpeg);
Response.clearcontent ();
Response.contenttype= "Image/jpeg";
Response.BinaryWrite (Ms. ToArray ());

G.dispose ();

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.