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.Drawing.Drawing2D;
Using System.IO;
Namespace ui._1209
{
Public partial class verification code: System.Web.UI.Page
{
#region///Custom method
#region///generate CAPTCHA picture private void Createcodeimage (string checkcode)
<summary>
Generate a Captcha picture
</summary>
private void Createcodeimage (string checkcode)
{
Determine if a random code exists
if (Checkcode = = NULL | | Checkcode.trim () = = String.Empty)
Return
int width= (int) math.ceiling ((Checkcode.length * 12.5));//Set the width of the canvas according to the Verification code
Set the size of the canvas
Bitmap btmimg = new Bitmap (width, 22);
Create a canvas
Graphics g = graphics.fromimage (btmimg);
Try
{
Create a random number
Random random = new random ();
Set the background color of the canvas
G.clear (Color.White);
Setting the background noise line
for (int i = 0; i < 2; i++)
{
int x1 = Random. Next (Btmimg.width);
int y1 = random. Next (Btmimg.height);
int x2 = random. Next (Btmimg.width);
int y2 = random. Next (Btmimg.height);
G.drawline (New Pen (Color.Black), x1, y1, x2, y2);
}
Set font
Font font = new Font ("Arial", 12f,fontstyle.bold);
Set the gradient color of text
LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0, 0, Btmimg.width, btmimg.height), Color.Blue, Color.darkred, 1.2f, true);
Create text
g.DrawString (Checkcode, Font, brush, 2, 2);
Set the foreground noise point
for (int i = 0; i <; i++)
{
int x = random. Next (Btmimg.width);
int y = random. Next (Btmimg.height);
Randomly generate noise points and set colors
Btmimg.setpixel (x, Y, Color.FromArgb (random. Next ()));
}
Set the border line of a picture
G.drawrectangle (New Pen (Color.silver), 0, 0, btmimg.width-1, btmimg.height-1);
Create a memory stream
MemoryStream ms = new MemoryStream ();
Save
Btmimg.save (ms,system.drawing.imaging.imageformat.gif);
Clear Stream
Response.clearcontent ();
Set the type of the output stream
Response.ContentType = "Image/gif";
Write it to the output stream
Response.BinaryWrite (Ms. ToArray ());
}
Finally
{
Reclaim Canvas Resources
G.dispose ();
Btmimg. Dispose ();
}
}
#endregion
#region///Generate a verification code and save it to a cookie private string Createcheckcode ()
<summary>
Generate a verification code and save it in a cookie
</summary>
<returns> string that returns the result </returns>
private String Createcheckcode ()
{
int number;
char code;
string checkcode = String.Empty;
Random random = new random ();
for (int i = 0; i < 4; i++)
{
Number = random. Next ();
Code = (char) (' 0 ' + (char) (number% 10));
Checkcode + = code. ToString ();
}
RESPONSE.COOKIES.ADD (New HttpCookie ("Checkcode", Checkcode));
return checkcode;
}
#endregion
#endregion
protected void Page_Load (object sender, EventArgs e)
{
Createcodeimage (Createcheckcode ());
}
}
}
Asp. NET Implementation Verification code