Web Verification Code

Source: Internet
Author: User

Foreground reference. aspx:

<%@ page language= "C #" autoeventwireup= "true" codefile= "YanZhengCode.aspx.cs" inherits= "Yanzhengcode"%>

<! DOCTYPE html>

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>


<script language= "javascript" type= "Text/javascript" >
function Suiji () {
document.getElementById ("Image1"). src = "gif_zl.aspx?chen=" + number;
}
</script>

</div>
</form>
</body>

YanZhengCode.cs

C # code

Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Drawing;
Using System.IO;
Using System;

public partial class YanZhengCode:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
String str = request["Chen"];
Buildcode ();

Shuimg ();
}

private void Buildcode ()
{

Color[] Strcolor = {color.red, Color.Blue, Color.brown, Color.Black, Color.tomato};

String[] Strfont = {"XXFarEastFont-Arial"};
Random rands = new Random ();
Bitmap images = new Bitmap (80, 26); Picture size
Graphics g = graphics.fromimage (images);
Font f = new font (strfont[0], 20); Font size

Brush Fill Color
Brush B = new SolidBrush (Color.tomato);
Brush b2 = new SolidBrush (strcolor[rands. Next (0, Strcolor. Length)]);
Brush B3 = new SolidBrush (color.red);
Brush b4 = new SolidBrush (Color.FromArgb (Color.green));
G.clear (color.wheat);

//Line Color
Pen p = new Pen (color.red, 0);
Pen P2 = new Pen (color.turquoise, 0);
Random rand = new Random ();
for (int i = 0; i < i++)
{
int y = rand. Next (images. Height);
int x = rand. Next (images. Width);
//g.drawline (p, 0, y, images). Width, y);
G.drawrectangle (p2, x, y, 1, 1);
}
//int degree = 5;
//point temppoint = new Point ();
//degree = ~degree + 1;
//g.rotatetransform (degree);
//temppoint.x = 20;
//temppoint.y = 4;

G.drawrectangle (p, 0, 0, images. Width-1, images. HEIGHT-1); Drawing area
String strcode = Rand. Next (1000, 10000). ToString ();
session["Yanzhencode"] = Strcode;
g.DrawString (strcode.substring (0, 1), F, B, 2,-1);
g.DrawString (strcode.substring (1, 1), F, B2, 19,-1);
g.DrawString (strcode.substring (2, 1), F, B3, 37,-1);
g.DrawString (Strcode.substring (3, 1), F, B4, 50,-1);
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
Images. Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.clearcontent ();
Response.ContentType = "Image/jpeg";
Response.BinaryWrite (Ms. ToArray ());
//response.cookies.add (New HttpCookie ("Chne", "6780"));
G.dispose ();
Images. Dispose ();
}

private void Shuimg ()
{
Response.ContentType = "image/*"; Set the send header as image data
Response.Clear (); Empty Send data
Random rand = new Random ();
Stream stream = OpenFile (Server.MapPath ("pic/" + Rand. Next (1, 6). ToString () + ". jpg"); Open File stream
Bitmap bit = new Bitmap (stream);
Bit. Save (Response.outputstream, System.Drawing.Imaging.ImageFormat.Jpeg);//saved to the output stream
Stream. Close ();
Bit. Dispose ();
Response.Flush (); Output to Client
}
}

Two *******************

Using System;  Using System.IO;  Using System.Drawing;  Using System.Drawing.Imaging;  Using System.Text;  Using System.Collections;  Using System.Web;  Using System.Web.UI;  Using System.Web.UI.WebControls;  Using System.Web.UI.WebControls.WebParts;  Using System.Web.UI.HtmlControls;  /**////<summary>///* * * ASP (C #) Generate Verification Code *//////File:GenerateCheckCode.aspx.cs////Author: Zhou Zhenxing (Zxjay)/////E-mail: [email protected]////DATE:07-04-10////</summary> Public Partial class GenerateCheckCode:System.Web.UI.Page ... {protected void Page_Load (object sender, EventArgs e) ... {string Chkcode = string.  Empty; Color list, for captcha, noise, noise color[] color = ...  {color.black, color.red, Color.Blue, Color.green, Color.orange, Color.brown, Color.brown, Color.DarkBlue}; Font list, for captcha string[] Font = ...  {"Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact"}; The character set of the captcha, stripped of some easily confusing characters char[] character =... {' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' J ', ' K ', ' L ', ' M ', ' N ', ' P ', ' R ', ' S ', ' T ',  ' W ', ' X ', ' Y '};  Random rnd = new Random (); Generate a CAPTCHA string for (int i = 0; i < 4; i++) ... {Chkcode + = Character[rnd. Next (character.  Length)];  } Bitmap BMP = new Bitmap (100, 40);  Graphics g = graphics.fromimage (BMP);  G.clear (Color.White); Draw noise line for (int i = 0; i < i++) ... {int x1 = rnd.      Next (100); int y1 = rnd.      Next (40); int x2 = rnd.      Next (100); int y2 = rnd.      Next (40); Color clr = color[rnd. Next (color.      Length)];  G.drawline (new Pen (CLR), x1, y1, x2, y2); }//Draw verification code string for (int i = 0; i < chkcode.length; i++) ... {String fnt = Font[rnd. Next (font.      Length)];      Font ft = new Font (FNT, 18); Color clr = color[rnd. Next (color.      Length)]; g.DrawString (Chkcode[i].  ToString (), FT, new SolidBrush (CLR), (float) I * 8, (float) 8); }//Draw noise for (int i = 0; i < i++) ... {int x = rnd. Next (BMP. Width); int y = rnd. Next (BMP.      Height); Color clr = color[rnd. Next (color.      Length)]; Bmp.  SetPixel (x, Y, CLR);  }//clear the page output cache, set the page without caching Response.Buffer = true;  Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds (0);  Response.Expires = 0;  Response.CacheControl = "No-cache";  Response.appendheader ("Pragma", "No-cache");  Writes the CAPTCHA picture to the memory stream and outputs it in "image/png" format MemoryStream ms = new MemoryStream (); Try ... {BMP.      Save (MS, Imageformat.png);      Response.clearcontent ();      Response.ContentType = "Image/png"; Response.BinaryWrite (Ms.  ToArray ()); } finally ... {//Explicitly release resource BMP.      Dispose ();  G.dispose (); }      }  }

Here's how to use it:
Create a new file named Generatecheckcode.aspx and copy the above code to the code file GenerateCheckCode.aspx.cs
Place the statement <asp:image id= "IMG1" runat= "Server" imageurl= "~/generatecheckcode.aspx"/> Where a verification code is required.

Web Verification Code

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.