Http://topic.csdn.net/u/20090713/07/e5dab5e9-c8e2-4fba-89c9-6cc523516117.html? 54480
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Drawing. drawing2d;
Using system. Drawing. imaging;
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 shop
{
/// <Summary>
/// Summary description for validatecode.
/// </Summary>
Public class validatecode: system. Web. UI. Page
{
/// <Summary>
/// Validation Code generated fromt these charaters.
/// Note: l, L 1 (number), O, O, 0 (number) are removed
/// </Summary>
Private const string strvalidatecodebound = "abcdefghijkmnpqrstuvwxyzabcdefghijklmnpqrstuvwxyz23456789 ";
Private Static string [] fonts = new string [] {"Helvetica ",
"Geneva ",
"Sans-serif ",
"Verdana ",
"Times New Roman ",
"Courier New ",
"Arial"
};
# Region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// Required method for designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
///
// event handler of page load
///
private void page_load (Object sender, system. eventargs e)
{< br> If (! Ispostback)
{< br> string str_validatecode = getrandomstring (6);
Session ["validatecode"] = str_validatecode;
Createimage (str_validatecode );
}
}
/// <Summary>
/// Generate random string
/// </Summary>
Private string getrandomstring (INT int_numberlength)
{
String valstring = string. empty;
Random therandomnumber = new random (INT) datetime. Now. ticks );
For (INT int_index = 0; int_index <int_numberlength; int_index ++)
Valstring + = strvalidatecodebound [therandomnumber. Next (strvalidatecodebound. Length-1)]. tostring ();
Return valstring;
}
/// <Summary>
/// Generate random color
/// </Summary>
Private color getrandomcolor ()
{
Random randomnum_first = new random (INT) datetime. Now. ticks );
System. Threading. thread. Sleep (randomnum_first.next (50 ));
Random randomnum_sencond = new random (INT) datetime. Now. ticks );
int int_red = randomnum_first.next (256);
int int_green = randomnum_sencond.next (256);
int int_blue = (int_red + int_green> 400 )? 0: 400-int_red-int_green;
int_blue = (int_blue> 255 )? 255: int_blue;
return color. fromargb (int_red, int_green, int_blue );
}< br>
///
// create Validation Code image
///
private void createimage (string str_validatecode)
{< br> int int_imagewidth = str_validatecode.length * 22;
random newrandom = new random ();
Bitmap thebitmap = new Bitmap (int_imagewidth + 6, 38 );
Graphics thegraphics = graphics. fromimage (thebitmap );
Thegraphics. Clear (color. White );
Drawline (thegraphics, thebitmap, newrandom );
Thegraphics. drawrectangle (new pen (color. lightgray, 1), 0, 0, thebitmap. Width-1, thebitmap. Height-1 );
For (INT int_index = 0; int_index <str_validatecode.length; int_index ++)
{
Matrix X = new matrix ();
X. shear (float) newrandom. Next (0,300)/1000-0.25f, (float) newrandom. Next (0,100)/1000-0.05f );
Thegraphics. Transform = X;
String str_char = str_validatecode.substring (int_index, 1 );
System. drawing. drawing2d. lineargradientbrush newbrush = new system. drawing. drawing2d. lineargradientbrush (New rectangle (0, 0, thebitmap. width, thebitmap. height), color. blue, color. darkred, 1.2f, true );
Point thepos = new point (int_index * 21 + 1 + newrandom. Next (3), 1 + newrandom. Next (13 ));
Font thefont = new font (fonts [newrandom. Next (fonts. Length-1)], newrandom. Next (14,18), fontstyle. Bold );
Thegraphics. drawstring (str_char, thefont, newbrush, thepos );
}
Drawpoint (thebitmap, newrandom );
Memorystream MS = new memorystream ();
Thebitmap. Save (MS, imageformat. PNG );
Response. clearcontent ();
Response. contenttype = "image/PNG ";
Response. binarywrite (Ms. toarray ());
Thegraphics. Dispose ();
Thebitmap. Dispose ();
Response. End ();
}
/// <Summary>
/// Draw line for Noise
/// </Summary>
Private void drawline (Graphics GFC, bitmap IMG, random ran)
{
For (INT I = 0; I <10; I ++)
{
Int X1 = ran. Next (IMG. width );
Int Y1 = ran. Next (IMG. Height );
Int X2 = ran. Next (IMG. width );
Int y2 = ran. Next (IMG. Height );
GFC. drawline (new pen (color. Silver), X1, Y1, X2, Y2 );
}
}
///
// draw point for noise
///
private void drawpoint (Bitmap IMG, random ran)
{< br> for (INT I = 0; I <30; I ++)
{< br> int x = ran. next (IMG. width);
int y = ran. next (IMG. height);
IMG. setpixel (X, Y, color. fromargb (ran. next ();
}
}
}
}