asp.net 2.0 HttpHandler implementation generates picture verification code (sample code download) __.net

Source: Internet
Author: User
Learn to tidy up a bit (a). The function uses HttpHandler to realize the picture verification Code (b). The code is as follows 1. The handler file Validateimagehandler.ashx code is as follows 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
Ten///<summary>
One///Validateimagehandler Build site authentication code feature
///</summary>
public class Validateimagehandler:ihttphandler, IRequiresSessionState
14 {
int intlength = 5; Length
String stridentify = "identify"; A random string stores key values so that they are stored in session
Validateimagehandler Public ()
18 {
19}
20
///<summary>
22///Build Verification Image Core code
///</summary>
///<param name= "HC" ></param>
public void ProcessRequest (HttpContext hc)
26 {
27//Set output stream picture format
HC. Response.ContentType = "Image/gif";
29
Bitmap B = new Bitmap (200, 60);
Graphics g = graphics.fromimage (b);
G.fillrectangle (New SolidBrush (Color.yellowgreen), 0, 0, 200, 60);
Font font = new Font (FONTFAMILY.GENERICSERIF, fontstyle.bold, GraphicsUnit.Pixel);
Random r = new Random ();
35
36//Legal random display character list
Panax Notoginseng String strletters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder s = new StringBuilder ();
39
40///draw randomly generated strings to the picture
A for (int i = 0; i < intlength i + +)
42 {
S.append (strletters.substring (r.next (0, strletters.length-1), 1));
g.DrawString (S[s.length-1]. ToString (), Font, New SolidBrush (Color.Blue), I *, R.next (0, 15));
45}
46
47//Generate interference lines
The pen pen = new Pen (new SolidBrush (Color.Blue), 2);
for (int i = 0; i < i + +)
50 {
G.drawline (pen, New Point (R.next (0, 199), R.next (0)), New Point (R.next (0, 199), R.next (0, 59));
52}
B.save (HC. Response.outputstream, imageformat.gif);
HC.  Session[stridentify] = s.tostring (); First save in session, verify consistency with user input
HC. Response.End ();
56
57}
58
///<summary>
60///Indicates whether such instances can be shared by multiple requests (reuse can improve performance)
///</summary>
The public bool IsReusable
63 {
The Get
65 {
The return true;
67}
68}
69}
70

2. Front page Code 1 < Asp:login ID = "Login1" runat = "server" BackColor = "#EFF3FB" bordercolor = "#B5C7DE" Bord erpadding = "4" BorderStyle = "Solid" borderwidth = "1px" font-names = "Verdana" font-size = "0.8em" Fo Recolor = "#333333" onauthenticate = "Login1_authenticate" >
2 < TitleTextStyle BackColor = "#507CD1" font-bold = "True" font-size = "0.9em" ForeColor = "White"/>
3 < InstructionTextStyle font-italic = "True" ForeColor = "Black"/>
4 < TextboxStyle font-size = "0.8em"/>
5 < Loginbuttonstyle BackColor = "white" bordercolor = "#507CD1" BorderStyle = "Solid" Borderwid th = "1px"
6 font-names = "Verdana" font-size = "0.8em" ForeColor = "#284E98"/>
7

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.