/************************************************************************/
/* Class Name: None
* Function: Generate Verification code picture
* Completion time: 2009-8-11
* Version Number: 1.0
* Modified Time:
* eping
* E-mail:[email protected]
/************************************************************************/
Using System;
Using System.Data;
Using System.Configuration;
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; Add Reference
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
Invoking a custom method to draw a verification code
Createcheckcodeimage (Generatecheckcode ());
}
private String Generatecheckcode ()
{
Create an integer type variable
int number;
Creating a character-type variable
char code;
Create a string variable and initialize it to null
string checkcode = String.Empty;
Create a Random Object
Random random = new random ();
Generate 4 numbers with a For loop
for (int i = 0; i < 4; i++)
{
Generate a random number
Number = random. Next ();
Convert a number to a character type
Code = (char) (' 0 ' + (char) (number% 10));
Checkcode + = code. ToString ();
}
//Add the generated random number to the cookie
RESPONSE.COOKIES.ADD (New HttpCookie ("Checkcode", Checkcode));
//return string
return Checkcode;
}
private void Createcheckcodeimage (string checkcode)
{
Determining that a string is not equal to null and NULL
if (Checkcode = = NULL | | Checkcode.trim () = = String.Empty)
Return
Create a Bitmap object
System.Drawing.Bitmap image = new System.Drawing.Bitmap ((int) math.ceiling ((Checkcode.length * 12.5)), 22);
Create a Graphics object
Graphics g = graphics.fromimage (image);
Try
{
Generate Random Generators
Random random = new random ();
Clear the background color of the picture
G.clear (Color.White);
Draw a picture of the background noise line
for (int i = 0; i < 2; i++)
{
int x1 = Random. Next (image. Width);
int x2 = random. Next (image. Width);
int y1 = random. Next (image. Height);
int y2 = random. Next (image. Height);
G.drawline (New Pen (Color.Black), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font ("Arial", N, (System.Drawing.FontStyle.Bold));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush (New Rectangle (0, 0, image.) Width, image. Height), Color.Blue, color.darkred, 1.2f, true);
g.DrawString (Checkcode, Font, brush, 2, 2);
Draw the foreground noise point of the picture
for (int i = 0; i <; i++)
{
int x = random. Next (image. Width);
int y = random. Next (image. Height);
Image. SetPixel (x, Y, Color.FromArgb (random. Next ()));
}
Draw the border line of a picture
G.drawrectangle (New Pen (Color.silver), 0, 0, image. Width-1, image. HEIGHT-1);
Output a picture to a page
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
Image. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF);
Response.clearcontent ();
Response.ContentType = "Image/gif";
Response.BinaryWrite (Ms. ToArray ());
}
Finally
{
G.dispose ();
Image. Dispose ();
}
}
}