C # Generate CAPTCHA program _php tutorial

Source: Internet
Author: User

Code:

Using System;
Using System.Data;
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;
public partial class Inc_ValidCode:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Verifycode v = new Verifycode ();
V.length = 4;
V.fontsize = 14;
V.chaos = true;
V.backgroundcolor = Color.White;
V.chaoscolor = Color.lightgray;
V.codeserial = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,a,b,c,d,e,f,g,h,i,j,k, L,m,n,o,p,q,r,s,t,u,v,w,x,y,z ";
v.colors = this.colors; ' "" "" "" "" "" "" "" "" "" "" "" "" "" "" "." "" "" "" "" "" "" "" "" "" "" "" "" "". " ```````````````````````````````````````````````````````````````````
V.fonts = this.fonts;
v.padding = 2;
String code = V.createverifycode (); Fetch Random Code
V.createimageonpage (code, this.) Context); Output picture
RESPONSE.COOKIES.ADD (New HttpCookie ("Fcvalidtcode", code. ToUpper ()));//Use cookies to get the value of the verification code
session["Bao_code"] = code. ToUpper ();
}
public class Verifycode
{
#region Verification Code Length (the length of the default 6 captcha) #region The length of the verification code (the length of the default 6 captcha)
int length = 6;
public int Length
{
get {return length;}
set {length = value;}
}
#endregion
#region captcha Font Size (default 40 pixels, you can modify it yourself) #region captcha font size (to show distortion, default 40 pixels, you can modify it yourself)
int fontSize = 30;
public int FontSize
{
get {return fontSize;}
set {fontSize = value;}
}
#endregion
#region Border complement (default 1 pixels) #region Border complement (default 1 pixels)
int padding = 2;
public int Padding
{
get {return padding;}
set {padding = value;}
}
#endregion
#region whether to output dryness point (not output by default) #region whether to output dryness point (not output by default)
BOOL Chaos = true;
public bool Chaos
{
get {return chaos;}
set {chaos = value;}
}
#endregion
#region the color of the output dryness point (default gray) #region The color of the output dryness point (default gray)
Color chaoscolor = Color.lightgray;
Public Color Chaoscolor
{
get {return chaoscolor;}
set {Chaoscolor = value;}
}
#endregion
#region Custom background color (default white) #region Custom background color (default white)
Color backgroundcolor = Color.White;
Public Color BackgroundColor
{
get {return backgroundcolor;}
set {backgroundcolor = value;}
}
#endregion
#region custom Random color array #region custom random color array
color[] Colors = {color.black, color.red, Color.darkblue, Color.green, Color.orange, Color.brown, Color.darkcyan, Color. Purple};
Public color[] Colors
{www.jbxue.com
get {return colors;}
set {colors = value;}
}
#endregion
#region Custom font array #region custom font array
string[] fonts = {"Arial", "Georgia"};
Public string[] Fonts
{
get {return fonts;}
set {fonts = value;}
}
#endregion
#region custom Random code string sequences (separated by commas) #region custom random code string sequences (separated by commas)
String codeserial = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,a,b,c,d,e,f,g,h,i,j, K,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z ";
public string codeserial
{
get {return codeserial;}
set {codeserial = value;}
}
#endregion
#region Waveform Filter Effect #region produces a waveform filter effect
Private Const Double PI = 3.1415926535897932384626433832795;
Private Const Double PI2 = 6.283185307179586476925286766559;
/**/
///


Sinusoidal wave distorted picture (Edit by 51aspx.com)
///
///Picture path
///True if Distortion is selected
///Amplitude multiples of the waveform, the greater the degree of distortion, the general is 3
///Starting phase of waveform, range of values [0-2*PI]
///
Public System.Drawing.Bitmap twistimage (Bitmap srcbmp, bool Bxdir, double dmultvalue, double dphase)
{
System.Drawing.Bitmap destbmp = new Bitmap (srcbmp.width, srcbmp.height);
Fills the bitmap background as white
System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage (destbmp);
Graph. FillRectangle (New SolidBrush (System.Drawing.Color.White), 0, 0, destbmp.width, destbmp.height);
Graph. Dispose ();
Double Dbaseaxislen = Bxdir? (double) Destbmp.height: (double) destbmp.width;
for (int i = 0; i < destbmp.width; i++)
{
for (int j = 0; J < Destbmp.height; J + +)
{//Www.jbxue.com
Double dx = 0;
DX = Bxdir? (PI2 * (double) j)/Dbaseaxislen: (PI2 * (double) i)/Dbaseaxislen;
DX + = Dphase;
Double dy = math.sin (dx);
Gets the color of the current point
int noldx = 0, Noldy = 0;
NOLDX = Bxdir? i + (int) (DY * dmultvalue): i;
Noldy = Bxdir? J:j + (int) (DY * dmultvalue);
System.Drawing.Color Color = Srcbmp.getpixel (i, j);
if (noldx >= 0 && noldx < destbmp.width
&& noldy >= 0 && noldy < destbmp.height)
{
Destbmp.setpixel (NOLDX, noldy, color);
}
}
}
return destbmp;
}
#endregion
#region Generate check code picture #region Generate check code picture
Public Bitmap Createimagecode (string code)
{
int fsize = FontSize;
int fwidth = fsize + Padding;
int imagewidth = (int) (code. Length * fwidth) + 4 + Padding * 2;
int imageheight = fsize * 2 + Padding;
System.Drawing.Bitmap image = New System.Drawing.Bitmap (imagewidth, imageheight);
Graphics g = graphics.fromimage (image);
G.clear (BackgroundColor);
Random rand = new Random ();
Add a randomly generated dry point to the background
if (this. Chaos)
{
Pen pen = new Pen (chaoscolor, 0);
int c = Length * 10;
for (int i = 0; i < C; i++)
{
int x = rand. Next (image. Width);
int y = rand. Next (image. Height);
G.drawrectangle (pen, x, Y, 1, 1);
}
}
int left = 0, top = 0, Top1 = 1, top2 = 1;
int n1 = (imageheight-fontsize-padding * 2);
int n2 = N1/4;
Top1 = n2;
Top2 = n2 * 2;
Font F;
Brush b;
int CIndex, Findex;
Verification code characters for random fonts and colors
for (int i = 0; i < code. Length; i++)
{
CIndex = Rand. Next (colors.length-1);
Findex = Rand. Next (fonts.length-1);
f = new System.Drawing.Font (Fonts[findex], fsize, System.Drawing.FontStyle.Bold);
b = new System.Drawing.SolidBrush (Colors[cindex]);
if (i% 2 = = 1)
{
top = TOP2;
}
Else
{
top = TOP1;
}
left = i * fwidth;
g.DrawString (code. Substring (i, 1), F, B, left, top);
}
Draw a border border color of Color.gainsboro
G.drawrectangle (New Pen (Color.gainsboro, 0), 0, 0, image. Width-1, image. HEIGHT-1);
G.dispose ();
Generate Waveform (ADD by 51aspx.com)
Image = Twistimage (image, True, 0, 4);
return image;
}
#endregion
#region will create a good picture output to the page #region will create a good picture output to the page
public void Createimageonpage (string code, HttpContext context)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
Bitmap image = this. Createimagecode (code);
Image. Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg);
Context. Response.clearcontent ();
Context. Response.ContentType = "Image/jpeg";
Context. Response.BinaryWrite (Ms. GetBuffer ());
Ms. Close ();
ms = NULL;
Image. Dispose ();
image = NULL;
}
#endregion
#region Generate random character code #region generate random character code
public string Createverifycode (int codelen)
{
if (Codelen = = 0)
{
Codelen = Length;
}
string[] arr = codeserial.split (', ');
String code = "";
int randvalue =-1;
Random rand = new Random (unchecked ((int) DateTime.Now.Ticks));
for (int i = 0; i < Codelen; i++)
{
Randvalue = Rand. Next (0, arr.) LENGTH-1);
Code + = Arr[randvalue];
}
return code;
}
public string Createverifycode ()
{
Return Createverifycode (0);
}
#endregion
}
}

http://www.bkjia.com/PHPjc/729841.html www.bkjia.com true http://www.bkjia.com/PHPjc/729841.html techarticle code: Using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using Sys Tem. web.security; Using System.Web.UI; Using System.Web.UI ...

  • 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.