//
// graphic verification code generation page
/// Abraxas@163.com
// reference method: (n is the number of digits of the Verification Code)
//
using system;
using system. collections;
using system. componentmodel;
using system. data;
using system. drawing;
using system. web;
using system. web. sessionstate;
using system. web. ui;
using system. web. UI. webcontrols;
using system. web. UI. htmlcontrols;
using system. io;
namespace Abraxas
{< br> ///
// abstract description of abraxasvalcode.
//
public class abraxasvalcode: system. web. UI. page
{< br> private void page_load (Object sender, system. eventargs E)
{< br> // place the user Code here to initialize the page
string valcode = randomstring (Int. parse (request. querystring ["N"]);
session. add ("valcode", valcode);
drawimage (valcode);
}
// Generate a random string
Private string randomstring (INT bit)
{
String chars = ", 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 [] chararray = chars. Split (',');
String result = "";
Random RDM = new random ();
For (INT I = 0; I <bit; I ++)
{
Result + = chararray [RDM. Next (36)];
}
Return result;
}
// Graphic the verification code
Private void drawimage (string valcode)
{
// Generate bitmap
Bitmap BMP = new Bitmap (valcode. length * 16, 25 );
Graphics G = graphics. fromimage (BMP );
Try
{
G. Clear (color. White );
// Define the color and font set
Color [] C = {color. Red, color. Green, color. Navy, color. darkcyan, color. Blue, color. Purple };
String [] fonts = {"Microsoft sans serif", "Times New Roman", "lucida console", "Arial", "verdana "};
// Draw the background Grid
Pen P = new pen (color. lightgray, 0 );
Int y = 5;
Do
{
G. drawline (p, 0, Y, BMP. Width, y );
Y + = 5;
}
While (Y <BMP. Height );
Int x = 5;
Do
{
G. drawline (p, X, 0, x, BMP. Height );
X + = 5;
}
While (x <BMP. width );
// Output verification code characters
Random RDM = new random ();
For (INT I = 0; I <valcode. length; I ++)
{
Int CI = RDM. Next (C. Length );
Int Fi = RDM. Next (fonts. Length );
Font F = new font (fonts [fi], 12, fontstyle. Bold );
Brush B = new solidbrush (C [CI]);
G. drawstring (valcode. substring (I, 1), F, B, 5 + (I * 14), RDM. Next (2, 5 ));
}
// Draw a border
G. drawrectangle (p, 0, 0, BMP. Width-1, BMP. Height-1 );
// output to page
memorystream MS = new memorystream ();
BMP. save (MS, system. drawing. imaging. imageformat. GIF);
response. cache. setnostore ();
response. clearcontent ();
response. contenttype = "image/GIF";
response. binarywrite (Ms. toarray ();
response. end ();
}< br> finally
{< br> G. dispose ();
BMP. dispose ();
}< BR >}
# code generated by region web forms designer
override protected void oninit (eventargs e)
{< br> //
// codegen: this call is ASP.. NET web form designer.
//
initializecomponent ();
base. oninit (E );
}< br>
///
// The method required by the designer. Do not use the code editor to modify the method.
/// the content of this method.
//
private void initializecomponent ()
{< br> This. load + = new system. eventhandler (this. page_load);
}< BR ># endregion
}