Learn Ihanlder class----------------about ASP. NET and IIS principles on-line Blog Harvest write a verification code with a general procedure to write a long time ago, but now look a little different feeling

Source: Internet
Author: User

Build a Web site new generic handler directly paste code:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.SessionState;
Using System.Drawing;
Using System.Text;
Using System.Drawing.Imaging;

Namespace Handlerstudy
{
<summary>
Summary description of ImageHandler
</summary>
public class Imagehandler:ihttphandler,irequiressessionstate
{

public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType = "Image/gif";
Bitmap basemap = new Bitmap (200, 60);
Graphics graph = graphics.fromimage (basemap);
Graph. FillRectangle (New SolidBrush (Color.White), 0, 0, 200, 60);
Font font = new Font (FONTFAMILY.GENERICSERIF, fontstyle.bold, GraphicsUnit.Pixel);
Random r = new Random ();
String letters = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String letter = String. Empty;
StringBuilder s = new StringBuilder ();
Add a random five verification code
for (int x = 0; x < 5; × x + +)
{
letter = Letters. Substring (r.next (0, letters. Length-1), 1);
S.append (letter);
Graph. DrawString (letter, font, New SolidBrush (Color.Black), X *, R.next (0, 15));
}

Confusing background

Pen Linepen = new Pen (new SolidBrush (Color.Black), 2);

for (int x = 0; x < 8; × x + +)
{
Graph. DrawLine (Linepen, New Point (R.next (0, 199), R.next (0, N)), New Point (R.next (0, 199), R.next (0, 59));
}


Save the picture to the output stream

Basemap. Save (context. Response.outputstream, imageformat.gif);
Context. session["Checkcode"] = s.tostring ();
Context. Response.End ();
}

public bool IsReusable
{
Get
{
return false;
}
}
}
}

Use this in a Web page:

The effect is as follows:

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.