. NET picture Verification code, final completion version __.net

Source: Internet
Author: User
Tags httpcontext

Yesterday I tried the example in the book, but under. NET 2005 there was always an error, prompted that the method rewrite could not be found, and then tried another method.

Set up a class, used to draw random characters, and then build a.aspx, in the pageload call this class, and then the login page of the img src attribute, set to this a.aspx, run it.

The code for the class.

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

Namespace Webhelper.picture
{
/**/
<summary>
Chinese Picture Verification Code
</summary>
public class Valids:System.Web.UI.Page
{
/**/
<summary>
Generate a random text picture, saved in session["Validcode"]
</summary>
<param name= "Count" > The number of words in the picture </param>
<returns> generated Text </returns>
public static void CreateImage (int count)
{
String Validcode = Gencode (count);
Createcheckcodeimage (Validcode);
httpcontext.current.session["Validcode"] = Validcode;
}

/**/
<summary>
Generate Random strings
</summary>
<param name= "num" > random out a few characters </param>
<returns> random out of the string </returns>
private static string gencode (int num)
{
String validatecode = "";

Random Number Object
Random Random = new Random ();

for (int i = 0; i < num; i++)
{
26:a-Z
int n = random. Next (26);

Convert numbers to uppercase letters
Validatecode + = (char) (n + 65);
}

return validatecode;


}

/**/
<summary>
Generate picture (Increase background noise line, foreground noise point)
</summary>
<param name= "Checkcode" > Random out String </param>
private static void Createcheckcodeimage (String checkcode)
{
if (checkcode.trim () = = "" | | Checkcode = null)
Return
System.Drawing.Bitmap image = new System.Drawing.Bitmap ((int) (CHECKCODE.LENGTH * 21.5), 22);
Graphics g = graphics.fromimage (image);
Try
{
Generate a random generator
Random Random = new Random ();

Empty picture background color
G.clear (Color.White);

Background noise line for painting pictures
int i;
for (i = 0; i < 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", (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);

Picture of the foreground noise point
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);
HttpContext.Current.Response.ClearContent ();
HttpContext.Current.Response.ContentType = "Image/gif";
HttpContext.Current.Response.BinaryWrite (Ms. ToArray ());
}
Catch
{
G.dispose ();
Image. Dispose ();
}
}
}
}

Join in the pageload of a.aspx

WebHelper.Picture.Valids.CreateImage (4);

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.