Image Verification Code Technology

Source: Internet
Author: User

Establish general functional Class Common.cs

Add a function in this class to generate a random string and convert the output of a string to a picture

The contents are as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Drawing;
Using System.IO;
Namespace Cartoon
{


public class Common
{
Gets a random string
public static string Getrandom (int n)
{
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghizklmnopqrstuvwxyz1234567890";
Random random = new random ();
Generate n-bit random numbers
string result = String. Empty;
for (int i = 1; I <= n; i++)
{
Result + = Str[random. Next (0, str. LENGTH-1)]. ToString ();
}
return result;


//String conversion picture
public static void DisplayImage (String str)
{
      Use a cookie or session pass value to verify that the verification code
//session
httpcontext.current.session["code" = str;
Cookies
system.web.httpcontext.current.response.cookies["code"). Value = str;       
//Create a picture area set width and height
Bitmap image = new Bitmap;
      Gets the area of the image where it is easy to add an element
Graphics g = graphics.fromimage (image);
      Set the picture background to white
G.clear (color.white);
      Draw lines to construct some blind spots, meaning that the picture is not clearly displayed
random random = new random ();
for (int i = 0; i < i++)
{
int x1 = random. Next (image. Width);
int y1 = random. Next (image. Height);
int x2 = random. Next (image. Width);
int y2 = random. Next (image. Height);
G.drawline (New Pen (color.silver), x1, y1, x2, y2);
}

Define font objects, set font size

Font font = new Font ("Blackbody", 12);
Defining brush Colors
SolidBrush brush = new SolidBrush (color.blueviolet);
Defines the position coordinate point of the brush write information for the upper-left corner (0,0) point
PointF point = new PointF (2, 2);
Write the string into the picture
g.DrawString (str, font, brush, point);
Draw a border for a picture
G.drawrectangle (New Pen (Color.silver), 0, 0, image. Width-1, image. HEIGHT-1);
Generates a memory stream object for easy storage and output of pictures
MemoryStream ms = new MemoryStream ();
Save the drawn picture to the memory stream in MS
Image. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF);
Output binary picture stream
HttpContext.Current.Response.BinaryWrite (Ms. ToArray ());



}
}

}

Verification of landing Page:

protected void Jll_click (object sender, EventArgs e)
{
String code = request.form["Check"];
Using cookies to determine
if (Code = = system.web.httpcontext.current.request.cookies["code"). Value)
Use session to determine
if (Code = = session["code"). ToString ())
{
Response.Write ("<script>alert (' right! ') </script> ");
}
Else
{
Response.Write ("<script>alert (' false! ') </script> ");
}
}

Image Verification Code Technology

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.