C # verification code,
Using System; using System. drawing; using System. drawing. drawing2D; using System. drawing. imaging; using System. IO; using System. text; using System. web; namespace Demo {// <summary> /// Captcha // </summary> public class Captcha {# region Private Field /// <summary> // Random Seed/ // </summary> private Random objRandom = new Random (); # endregion # region Public Property # region verification code length // <summary> // verification code length/ /// </Summary> private int length = 4; // <summary> // verification code length (4 by default) /// </summary> public int Length {get {return this. length;} set {this. length = value ;}# endregion # region verification code string /// <summary> /// verification code string /// </summary> private string verifyCodeText = null; /// <summary> /// verification code string /// </summary> public string VerifyCodeText {get {return this. verifyCodeText;} set {this. verifyCodeText = Value ;}# endregion # Add lowercase letters to region // <summary> // Add lowercase letters to region /// </summary> private bool addLowerLetter = false; /// <summary> /// whether to add lowercase letters (excluding o) /// </summary> public bool AddLowerLetter {get {return this. addLowerLetter;} set {this. addLowerLetter = value ;}# endregion # whether to add uppercase letters to region // <summary> // whether to add uppercase letters // </summary> private bool addUpperLetter = false; /// <summary> /// whether to add Uppercase letters (excluding O) /// </summary> public bool AddUpperLetter {get {return this. addUpperLetter;} set {this. addUpperLetter = value ;}# endregion # region font size // <summary> // font size /// </summary> private int fontSize = 18; /// <summary> /// font size (18 by default) /// </summary> public int FontSize {get {return this. fontSize;} set {this. fontSize = value ;}# endregion # region font color // <summary> // font color /// </Summary> private Color fontColor = Color. blue; // <summary> // font Color (Blue by default) /// </summary> public Color FontColor {get {return this. fontColor;} set {this. fontColor = value ;}# endregion # region font type // <summary> // font type // </summary> private string fontFamily = "Verdana "; /// <summary> /// font type (Verdana by default) /// </summary> public string FontFamily {get {return this. fontFamily;} set {This. fontFamily = value ;}# endregion # region background Color /// <summary> // background Color /// </summary> private Color backgroundColor = Color. aliceBlue; // <summary> // background Color (AliceBlue by default) /// </summary> public Color BackgroundColor {get {return this. backgroundColor;} set {this. backgroundColor = value ;}# endregion # region foreground noise count // <summary> // foreground noise count // </summary> private int foreNoisePointCount = 2; /// <Summary> /// foreground noise count (2 by default) /// </summary> public int ForeNoisePointCount {get {return this. foreNoisePointCount;} set {this. foreNoisePointCount = value ;}# endregion # Rotation Angle of the region random code /// <summary> // Rotation Angle of the random code /// </summary> private int randomAngle = 45; /// <summary> // The Rotation Angle of the random code (40 degrees by default) /// </summary> public int RandomAngle {get {return this. randomAngle;} set {this. randomAngle = v Alue ;}# endregion # region Constructor Method // <summary> // Constructor Method /// </summary> public Captcha () {this. getText () ;}# endregion # region Private Method // <summary> // obtain the verification code string /// </summary> private void GetText () {// if (String. isNullOrEmpty (this. verifyCodeText) {StringBuilder objStringBuilder = new StringBuilder (); // Add the numbers 1-9 for (int I = 1; I <= 9; I ++) {objS TringBuilder. append (I. toString ();} // Add an upper-case A-Z that does not include O if (this. addUpperLetter) {char temp = ''; for (int I = 0; I <26; I ++) {temp = Convert. toChar (I + 65); // if the generated letter is not 'O' if (! Temp. equals ('O') {objStringBuilder. append (temp) ;}}// add lowercase letters a-z, excluding o if (this. addLowerLetter) {char temp = ''; for (int I = 0; I <26; I ++) {temp = Convert. toChar (I + 97); // if the generated letter is not 'O' if (! Temp. equals ('O') {objStringBuilder. append (temp) ;}}// generate the verification code string {int index = 0; for (int I = 0; I <length; I ++) {index = objRandom. next (0, objStringBuilder. length); this. verifyCodeText + = objStringBuilder [index]; objStringBuilder. remove (index, 1) ;}}/// <summary> // obtain the verification code picture /// </summary> private Bitmap GetImage () {Bitmap result = null; // create a drawing result = new Bitmap (this. verifyCodeText. Length * 16, 25); using (Graphics objGraphics = Graphics. fromImage (result) {objGraphics. smoothingMode = SmoothingMode. highQuality; // clear the entire drawing surface and fill the objGraphics with the specified background color. clear (this. backgroundColor); // create a paint brush using (SolidBrush objSolidBrush = new SolidBrush (this. fontColor) {this. addForeNoisePoint (result); this. addBackgroundNoisePoint (result, objGraphics); // text center StringFormat objStringFormat = new StringF Ormat (StringFormatFlags. noClip); objStringFormat. alignment = StringAlignment. center; objStringFormat. lineAlignment = StringAlignment. center; // Font objFont = new Font (this. fontFamily, objRandom. next (this. fontSize-3, this. fontSize), FontStyle. regular); // The verification code is rotated to prevent the machine from recognizing char [] chars = this. verifyCodeText. toCharArray (); for (int I = 0; I <chars. length; I ++) {// float angle = objRandom. nex T (-this. randomAngle, this. randomAngle); objGraphics. translateTransform (12, 12); objGraphics. rotateTransform (angle); objGraphics. drawString (chars [I]. toString (), objFont, objSolidBrush,-2, 2, objStringFormat); objGraphics. rotateTransform (-angle); objGraphics. translateTransform (2,-12) ;}} return result ;} /// <summary> /// add foreground noise /// </summary> /// <param name = "objBitmap"> </param> private void DdForeNoisePoint (Bitmap objBitmap) {for (int I = 0; I <objBitmap. width * this. foreNoisePointCount; I ++) {objBitmap. setPixel (objRandom. next (objBitmap. width), objRandom. next (objBitmap. height), this. fontColor );}} /// <summary> /// add background noise /// </summary> /// <param name = "objBitmap"> </param> /// <param name = "objGraphics"> </param> private void AddBackgroundNoisePoint (Bitmap objBitmap, graphics objGr Aphics) {using (Pen objPen = new Pen (Color. azure, 0) {for (int I = 0; I <objBitmap. width * 2; I ++) {objGraphics. drawRectangle (objPen, objRandom. next (objBitmap. width), objRandom. next (objBitmap. height), 1, 1) ;}}# endregion # region Public Method public void Output (HttpResponse objHttpResponse) {using (Bitmap objBitmap = this. getImage () {if (objBitmap! = Null) {using (MemoryStream objMS = new MemoryStream () {objBitmap. save (objMS, ImageFormat. jpeg); HttpContext. current. response. clearContent (); HttpContext. current. response. contentType = "image/Jpeg"; HttpContext. current. response. binaryWrite (objMS. toArray (); HttpContext. current. response. flush (); HttpContext. current. response. end () ;}}# endregion }}