Asp.net uses ashx to generate a graphical verification code, asp. netashx

Source: Internet
Author: User

Asp.net uses ashx to generate a graphical verification code, asp. netashx

This example describes how asp.net uses ashx to generate a graphic verification code. We will share this with you for your reference. The details are as follows:

You can understand the benefits of verification codes. On the Internet, I saw someone Directly Writing the verification code on the aspx page. That is to say, this method is not scientific to request a verification code. As shown below

<form id="form1" runat="server">  <div>    <asp:Image ID="Image1" runat="server" ImageUrl="Default.aspx" />    <br />    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />  </div></form>

This Code seems to be a waste of code writing, and it is really depressing to write the code. Do not write a script for the next switch.

The following describes how to implement this function.

1. Write an ashx to generate a graphic verification code.

Using System; using System. collections; using System. data; using System. linq; using System. web; using System. web. services; using System. web. services. protocols; using System. xml. linq; using System. web. sessionState; using System. drawing; namespace usechecknum. ashx {// <summary> /// $ Brief description of codebehindclassname $ /// </summary> [WebService (Namespace =" http://tempuri.org/ ")] [WebServiceBinding (ConformsTo = WsiProfiles. basicProfile1_1)] public class doCreateNum: IHttpHandler, IRequiresSessionState {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/html"; string checkCode = GetValidation (5); // generates five random Verification Code characters: context. session ["Code"] = checkCode; // Save the string to the Session so that System can be verified as needed. drawing. bitmap image = new System. drawing. bitmap (70, 22); Graphics g = Graphics. fromImage (image); try {// generate Random generator random Random = new Random (); // clear the image background color g. clear (Color. white); // specifies the background noise line of the image. int I; for (I = 0; I <25; I ++) {int x1 = random. next (image. width); int x2 = random. next (image. width); int y1 = random. next (image. height); int y2 = random. next (image. height); g. drawLine (new Pen (Color. silver), x1, y1, x2, y2);} Font font = new System. drawing. font ("Arial", 12, (System. drawing. fontStyle. bold); System. drawing. drawing2D. linearGradientBrush brush = new System. drawing. drawing2D. linearGradientBrush (new Rectangle (0, 0, image. width, image. height), Color. blue, Color. darkRed, 1.2F, true); g. drawString (checkCode, font, brush, 2, 2); // The foreground noise of the picture. g. drawRectangle (new Pen (Color. silver), 0, 0, image. width-1, image. height-1); System. IO. memoryStream MS = new System. IO. memoryStream (); image. save (MS, System. drawing. imaging. imageFormat. gif); context. response. clearContent (); context. response. contentType = "image/Gif"; context. response. binaryWrite (ms. toArray ();} finally {g. dispose (); image. dispose () ;}} public string GetValidation (int num) {string str = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // "or" string validatecode = ""; random rd = new Random (); for (int I = 0; I <num; I ++) {validatecode + = str. substring (rd. next (0, str. length), 1) ;}return validatecode;} public bool IsReusable {get {return false ;}}}}

2. The verification code is displayed on the page. Because the image is generated, you can directly write the verification code in the label. You only need to write a simple script and then click the mouse to switch the verification code.

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "index. aspx. cs" Inherits = "usechecknum. _ Default" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

After talking for half a day, it's time to see what the generated verification code looks like.

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.