1. First create a verification class CAPTCHA
Public class CAPTCHA
{
Private string text;
Private int width;
Private int height;
Private string familyname;
Private bitmap image;
Private Static random = new random ();
Public String familyname
{
Get {return familyname ;}
Set {familyname = value ;}
}
Public String text
{
Get {return this. Text ;}
Set {text = value ;}
}
Public bitmap image
{
Get
{
If (! String. isnullorempty (text) & height> 0 & width> 0)
Generateimage ();
Return this. image;
}
}
Public int width
{
Get {return this. width ;}
Set {width = value ;}
}
Public int height
{
Get {return this. height ;}
Set {Height = value ;}
}
Public CAPTCHA ()
{
}
~ CAPTCHA ()
{
Dispose (false );
}
Public void dispose ()
{
GC. suppressfinalize (this );
This. Dispose (true );
}
Protected virtual void dispose (bool disposing)
{
If (disposing)
// Dispose of the bitmap.
This. image. Dispose ();
}
Private void setdimensions (INT width, int height)
{
// Check the width and height.
If (width <= 0)
Throw new argumentoutofrangeexception ("width", width, "argument out of range, must be greater than zero .");
If (height <= 0)
Throw new argumentoutofrangeexception ("height", height, "argument out of range, must be greater than zero .");
This. width = width;
This. Height = height;
}
Private void setfamilyname (string familyname)
{
Try
{
Font font = new font (this. familyname, 16f );
This. familyname = familyname;
Font. Dispose ();
}
Catch
{
This. familyname = system. Drawing. fontfamily. genericserif. Name;
}
}
Public void generateimage ()
{
// ---- Without an image for the background ------
// Create a new 32-bit bitmap image.
Bitmap bitmap = new Bitmap (this. Width, this. Height, pixelformat. format32bppargb );
// Create a graphics object for drawing.
Graphics G = graphics. fromimage (Bitmap );
G. smoothingmode = smoothingmode. antialias;
Rectangle rect = new rectangle (0, 0, this. Width, this. Height );
// Fill in the background.
// Hatchbrush = new hatchbrush (hatchstyle. smallconfetti, colortranslator. fromhtml ("#607f20"), colortranslator. fromhtml ("# ffea00 "));
// Hatchbrush = new hatchbrush (hatchstyle. trellis, colortranslator. fromhtml ("# ffffff"), colortranslator. fromhtml ("#607f20 "));
Hatchbrush = new hatchbrush (hatchstyle. smallconfetti, color. fromargb (114,172,236), color. fromargb (161,214,255 ));
// Color. fromargb (76,136,198) Medium
// Color. fromargb (0,79, 136) dark
// Color. fromargb (114,172,236) medium-light
// Color. fromargb (135,188,254) Light
// Color. fromargb (161,214,255) really light
G. fillrectangle (hatchbrush, rect );
// ---- With a background image ----------
// String bgpath = httpcontext. Current. server. mappath ("captchabg.bmp ");
// Bitmap bitmap = new Bitmap (bgpath );
// Graphics G = graphics. fromimage (Bitmap );
// G. smoothingmode = smoothingmode. antialias;
// Hatchbrush = NULL;
// Rectangle rect = new rectangle (0, 0, this. Width, this. Height );
//-----------------------------------------
// Set up the text font.
Sizef size;
Float fontsize = This. height + 4;
Font font;
// Adjust the font size until the text fits within the image.
Do
{
Fontsize --;
Font = new font (this. familyname, fontsize, fontstyle. Bold );
Size = G. measurestring (this. Text, font );
} While (size. width> This. width );
// Set up the text format.
Stringformat format = new stringformat ();
Format. Alignment = stringalignment. Center;
Format. linealignment = stringalignment. Center;
// Create a path using the text and warp it randomly.
Graphicspath Path = new graphicspath ();
Path. addstring (this. Text, Font. fontfamily, (INT) font. style, Font. Size, rect, format );
Float v = 4f;
Pointf [] points =
{
New pointf (random. Next (this. width)/V, random. Next (this. Height)/V ),
New pointf (this. Width-random. Next (this. width)/V, random. Next (this. Height)/V ),
New pointf (random. Next (this. width)/V, this. Height-random. Next (this. Height)/V ),
New pointf (this. Width-random. Next (this. width)/V, this. Height-random. Next (this. Height)/V)
};
Matrix matrix = new matrix ();
Matrix. Translate (0f, 0f );
Path. Warp (points, rect, matrix, warpmode. Perspective, 0f );
// Draw the text.
Hatchbrush = new hatchbrush (hatchstyle. smallconfetti, colortranslator. fromhtml ("#000000"), colortranslator. fromhtml ("#000000 "));
// White numbers
// Hatchbrush = new hatchbrush (hatchstyle. smallconfetti, colortranslator. fromhtml ("# ffffff"), colortranslator. fromhtml ("# ffffff "));
// Yellow numbers
// Hatchbrush = new hatchbrush (hatchstyle. smallconfetti, colortranslator. fromhtml ("# ffea00"), colortranslator. fromhtml ("# ffea00 "));
G. fillpath (hatchbrush, PATH );
/// Add some random noise.
Int M = math. Max (this. Width, this. Height );
For (INT I = 0; I <(INT) (This. Width * This. Height/30f); I ++)
{
Int x = random. Next (this. width );
Int y = random. Next (this. Height );
Int W = random. Next (M/50 );
Int H = random. Next (M/50 );
G. fillellipse (hatchbrush, X, Y, W, H );
}
// Clean up.
Font. Dispose ();
Hatchbrush. Dispose ();
G. Dispose ();
// Set the image.
This. Image = bitmap;
}
Public static string generaterandomcode ()
{
String S = "";
For (INT I = 0; I <6; I ++)
S = string. Concat (S, random. Next (10). tostring ());
Return S;
}
}
2. Create a captcharesult class inherited from actionresult in the model folder.
Public class captcharesult: actionresult
{
Public String _ captchatext;
Public captcharesult (string captchatext)
{
_ Captchatext = captchatext;
}
Public override void executeresult (controllercontext context)
{
CAPTCHA c = new CAPTCHA ();
C. Text = _ captchatext;
C. width = 200;
C. Height = 50;
C. familyname = "century schoobook ";
Httpcontextbase cb = context. httpcontext;
CB. response. Clear ();
CB. response. contenttype = "image/JPEG ";
C. image. Save (CB. response. outputstream, imageformat. JPEG );
C. Dispose ();
}
}
3. Add two actions (getcaptcha and index with parameters) to homecontroller. CS)
Public captcharesult getcaptcha ()
{
String captchatext = CAPTCHA. generaterandomcode ();
Httpcontext. session. Add ("CAPTCHA", captchatext );
Return new captcharesult (captchatext );
}
[Httppost]
Public actionresult index (string CAPTCHA)
{
If (CAPTCHA = httpcontext. session ["CAPTCHA"]. tostring ())
Viewdata ["message"] = "CAPTCHA challenge was successful! ";
Else
Viewdata ["message"] = "CAPTCHA challenge failed-Please try again! ";
Return view ();
}
4. Add a code section above and below in the index View
<% Using (html. beginform ("Index", "home") {%>
<P> </P>
<P> Please enter the number above: </P>
<P> <% = html. Textbox ("CAPTCHA") %> </P>
<P> <input type = "Submit" value = "Submit"/> </P>
<% }%>
OK. This is the final result.