1. Compile CreateImage. cs
2. output the verification Image in Image. aspx on the page, that is, call the function: CreateImage. DrawImage ();
3. Use src = Image. aspx in the Image
4. Check the Session: Session ["CheckCode"]
The following is a verification code generation class: CreateImage. cs
Using System;
Using System. IO;
Using System. Web;
Using System. Drawing;
Namespace ZCJ168.App _ Code
{
/// <Summary>
/// Verification Code Module
/// </Summary>
Public class CreateImage
{
Public static void DrawImage ()
{
CreateImage img = new CreateImage ();
HttpContext. Current. Session ["CheckCode"] = img. RndNum (5 );
Img. CreateImages (HttpContext. Current. Session ["CheckCode"]. ToString ());
}
/// <Summary>
/// Generate verification Image
/// </Summary>
/// <Param name = "checkCode"> Verification character </param>
Private void CreateImages (string checkCode)
{
Int iwidth = (int) (checkCode. Length * 14 );
System. Drawing. Bitmap image = new System. Drawing. Bitmap (iwidth, 23 );
Graphics g = Graphics. FromImage (image );
G. Clear (Color. White );
// Define the color
Color [] c = {Color. Black, Color. Red, Color. DarkBlue, Color. Green, Color. Orange, Color. Brown, Color. DarkCyan, Color. Purple };
// Define the font
String [] font = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", ""};
Random rand = new Random ();
// Random output noise
For (int I = 0; I <50; I ++)
{
Int x = rand. Next (image. Width );
Int y = rand. Next (image. Height );
G. DrawRectangle (new Pen (Color. GreenYellow, 0), x, y, 1, 1 );
}
// Output verification code characters of different fonts and colors
For (int I = 0; I <checkCode. Length; I ++)
{
Int cindex = rand. Next (7 );
Int findex = rand. Next (5 );
Font f = new System. Drawing. Font (font [findex], 10, System. Drawing. FontStyle. Bold );
Brush B = new System. Drawing. SolidBrush (c [cindex]);
Int ii = 4;
If (I + 1) % 2 = 0)
{
Ii = 2;
}
G. DrawString (checkCode. Substring (I, 1), f, B, 3 + (I * 12), ii );
}
// Draw a border
G. DrawRectangle (new Pen (Color. Black, 0), 0, 0, image. Width-1, image. Height-1 );
// Output to the browser
System. IO. MemoryStream MS = new System. IO. MemoryStream ();
Image. Save (MS, System. Drawing. Imaging. ImageFormat. Jpeg );
HttpContext. Current. Response. ClearContent ();
// Response. ClearContent ();
HttpContext. Current. Response. ContentType = "image/Jpeg ";
HttpContext. Current. Response. BinaryWrite (ms. ToArray ());
G. Dispose ();
Image. Dispose ();
}
/// <Summary>
/// Generate a random string
/// </Summary>
/// <Param name = "VcodeNum"> Number of Generated letters </param>
/// <Returns> string </returns>
Private string RndNum (int VcodeNum)
{
String Vchar = "a, B, c, d, e, f, g, h, I, j, k, l, m, n, o, p, q, r, s, t, y, v, 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 ";
String [] VcArray = Vchar. Split (',');
String VNum = ""; // StringBuilder is not required because the string is short.
Int temp =-1; // record the previous random value. Avoid producing several identical random numbers.
// Use a simple algorithm to ensure different random numbers are generated
Random rand = new Random ();
For (int I = 1; I <VcodeNum + 1; I ++)
{
If (temp! =-1)
{
Rand = new Random (I * temp * unchecked (int) DateTime. Now. Ticks ));
}
Int t = rand. Next (VcArray. Length );
If (temp! =-1 & temp = t)
{
Return RndNum (VcodeNum );
}
Temp = t;
VNum + = VcArray [t];
}
Return VNum;
}
}
}