First look at the effect implementation (because the GIF screen recording software is a real-time search, some lost)
The main code is to draw the implementation of the Verification Code class
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Drawing;
Using System.IO; Namespace Securitycodepic {public class Drawingsecuritycode {///<summary>///generates a CAPTCHA and returns///</summary&
Gt
<returns></returns> public string Getsecuritycode (int n) {string code = Generatecheckcode (n);
Createcheckcodeimage (code);
return code; ///<summary>///dynamically generate a specified number of random numbers or letters///</summary>///<param name= "num" > Integer </param>/// <returns> return validation code string </returns> private string generatecheckcode (int num) {int number;//define variable char code
; string checkcode = String.Empty; Empty string, read-only Random Random = new Random (); Define a random variable instance for (int i=0 i < num;i++) {//Generate a specified number of random numbers or letters with the FOR loop = random. Next ();
Returns a nonnegative random number that is less than the specified maximum value next has three constructors if (number% 2 = 0) {//produces a single digit code = (char) (' 0 ' + (char) (#% 10)); else {//Generate aCapital Letter code = (char) (' A ' + (char) (number% 26)); } Checkcode + = code.
ToString ();
return checkcode; ///<summary>///generates a CAPTCHA image based on a CAPTCHA string///</summary>///<param name= "Checkcode" > Authenticode code string </param > private void Createcheckcodeimage (string checkcode) {if Checkcode = null | | Checkcode.trim () = String.emp
TY) return; Reference System.Drawing class Library Bitmap myimage = new Bitmap (80, 30);//Generate a bitmap of the specified size Graphics Graphics = Graphics.fromimage (Myima GE); Generates a canvas try {graphics from a bitmap. Clear (Color.White); Clears the entire drawing surface and fills with the specified background color, setting the background color to white Random Random = new Random (); Instantiate a pseudo-random number generator/picture foreground noise point, here are 100 for (int i = 0; i < i++) {int x = random.
Next (Myimage.width); int y = random.
Next (Myimage.height); Myimage.setpixel (x, Y, Color.FromArgb) (random. Next ());//Specify the color of the pixel at coordinates x,y}//Picture background noise line, here is 2 for (int i = 0; i < 2; i++) {int x1 = random.
Next (Myimage.width); int x2 = RANdom.
Next (Myimage.width); int y1 = random.
Next (Myimage.height); int y2 = random.
Next (Myimage.height); Graphics. DrawLine (New Pen (Color.Black), x1, y1, x2, y2); Draws a coordinate x1,y1 to the specified color line of the coordinate x2,y2, where the line is black} font font = new Font ("Arial", fontstyle.bold);
Defines a specific text format where the font is Arial, the size is 15, the font bold//generates a LinearGradientBrush instance---a linear gradient based on the rectangle, the starting color, the ending color, and the orientation angle System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush (New Rect
Angle (0, 0, Myimage.width, myimage.height),//at coordinates 0, 0 instantiate a rectangle with the same size as MyImage Color.Blue, Color.Red, 1.2f, true); Draws a text string graphics.
DrawString (checkcode, font, brush, 2, 2); Draws a rectangle with a coordinate pair, width, and height specified---the Border line graphics the picture.
DrawRectangle (New Pen (Color.silver), 0, 0, myimage.width-1, myimage.height-1);
Create a stream of its supporting memory for memory MemoryStream ms = new MemoryStream (); Saves this image to the specified stream in the specified format Myimage.save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF); This is saved to memory in GIF format httpcontext.current. Response.clearcontent (); Clears all content output in the buffer stream HttpContext.Current.Response.ContentType = "Image/gif"; Gets or sets the HTTP MIME type HttpContext.Current.Response.BinaryWrite (ms) for the output stream. ToArray ()); Writes a binary string to the HTTP output stream} finally {//free resource graphics.
Dispose ();
Myimage.dispose ();
}
}
}
}
then use the Securitycode.ashx file to invoke the above class method implementation
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Namespace Securitycodepic
{
///<summary>
///SecurityCode1 Summary description
///</summary>
public class Securitycode1:ihttphandler
{public
void ProcessRequest (HttpContext context)
{
Drawingsecuritycode sc = new Drawingsecuritycode ();
String Securitycode = sc. Getsecuritycode (6);
}
public bool IsReusable
{
get
{return
false;
}}}
The last is a reference to the ASP.net page picture path.
<%@ Page language= "C #" autoeventwireup= "true" codebehind= "Securitycode_ Test.aspx.cs "inherits=" Securitycodepic.securitycode_test "%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
The above is the whole of this article, yes, there is the source code to share to everyone, welcome to download.
Source sharing: ASP.net verification Code implementation