Use the ashx file to generate a verification code Image

Source: Internet
Author: User

In vs2005, you can directly create a. ashx file. Its project is called general processing.Program,. The ashx file is generally used to process only returned data. Second, it generally does not return data, such as dynamic generation of images or text. The following figure shows a dynamic generation of Verification Code image on the qingyueer blog.Code.

// Draw the verification code image validateimagehandler. ashx

1 <% @ Webhandler Language = " C # " Class = " Validateimagehandler "   %>
2
3 Using System;
4 Using System. Web;
5 Using System. Web. sessionstate;
6 Using System. drawing;
7 Using System. Drawing. imaging;
8 Using System. text;
9
10 ///   <Summary>
11 /// Validateimagehandler: website verification code generation function
12 ///   </Summary>
13 Public   Class Validateimagehandler: ihttphandler, irequiressessionstate
14 {
15 Int Intlength =   5 ; // Length
16 String Stridentify =   " Identify " ; // Store the key value of a random string to store it in the session.
17 Public Validateimagehandler ()
18 {
19 }
20
21 ///   <Summary>
22 /// Generate verification image core code
23 ///   </Summary>
24 ///   <Param name = "HC"> </param>
25 Public   Void Processrequest (httpcontext HC)
26 {
27 // Set the output stream image format
28 HC. response. contenttype =   " Image/GIF " ;
29
30 Bitmap B =   New Bitmap ( 200 , 60 );
31 Graphics g = Graphics. fromimage (B );
32 G. fillrectangle ( New Solidbrush (color. yellowgreen ), 0 , 0 , 200 , 60 );
33 Font font =   New Font (fontfamily. genericserif, 48 , Fontstyle. Bold, graphicsunit. pixel );
34 Random R =   New Random ();
35
36 // List of valid and random characters
37 String Strletters =   " Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890 " ;
38 Stringbuilder s =   New Stringbuilder ();
39
40 // Draw a random string to the image.
41 For ( Int I =   0 ; I < Intlength; I ++ )
42 {
43 S. append (strletters. substring (R. Next ( 0 , Strletters. Length -   1 ), 1 ));
44 G. drawstring (s [S. Length -   1 ]. Tostring (), Font, New Solidbrush (color. Blue), I *   38 , R. Next ( 0 , 15 ));
45 }
46
47 // Generate interference lines
48 Pen =   New Pen ( New Solidbrush (color. Blue ), 2 );
49 For ( Int I =   0 ; I <   10 ; I ++ )
50 {
51 G. drawline (pen, New Point (R. Next ( 0 , 199 ), R. Next ( 0 , 59 )), New Point (R. Next ( 0 , 199 ), R. Next ( 0 , 59 )));
52 }
53 B. Save (HC. response. outputstream, imageformat. GIF );
54 HC. session [stridentify] = S. tostring (); // Saved in session to verify consistency with user input
55 HC. response. End ();
56
57 }
58
59 ///   <Summary>
60 /// Indicates whether such instances can be shared by multiple requests (reuse can improve performance)
61 ///   </Summary>
62 Public   Bool Isreusable
63 {
64 Get
65 {
66 Return   True ;
67 }
68 }
69 }

// Reference the ashx file in the front-end code

.....

....

Here, because the default *. ASAH File Processing type is used, this type of default registration already exists in the machine. config file,

Because registration is not required here

1 < Httphandlers >
2 < Add verb = " * " Path = " *. ASAH " Type = " System. Web. UI. simplehandlerfactory "   />
3 </ Httphandlers >
4

 Note::

1. Registration will not cause errors and will overwrite the configuration of the machine. config file.

2. If you register multiple times in the same configuration file, the default latter will also overwrite the former.

3. If other formats are not registered by default, register them in the web. config file.

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.