Code:
public string fncDrawCaptcha(string path)
{
//*** Editable Values
int[] BackgroundColor = new int[] { 255, 255, 255 };// The 3 numbers represent in order RED, GREEN, BLUE for the captcha's background color
bool RandomBackgroundNoiseColor = true;// True / False. If you choose True, BackgroundNoiseColor will not apply
bool RandomTextColor = true;// True / False. If you choose True, TextColor will not apply
int[] BackgroundNoiseColor = new int[] { 150, 150, 150 };// The 3 numbers represent in order RED, GREEN, BLUE
int[] TextColor = new int[] { 200, 200, 200 };// The 3 numbers represent in order RED, GREEN, BLUE
HatchStyle BackgroundNoiseTexture = HatchStyle.Min;// replace ".Min" with any of the following: Horizontal, Vertical, ForwardDiagonal, BackwardDiagonal, Cross, DiagonalCross, Percent05, Percent10, Percent20, Percent25, Percent30, Percent40, Percent50, Percent60, Percent70, Percent75, Percent80, Percent90, LightDownwardDiagonal, LightUpwardDiagonal, DarkDownwardDiagonal, DarkUpwardDiagonal, WideDownwardDiagonal, WideUpwardDiagonal, LightVertical, LightHorizontal, NarrowVertical, NarrowHorizontal, DarkVertical, DarkHorizontal, DashedDownwardDiagonal, DashedUpwardDiagonal, DashedHorizontal, DashedVertical, SmallConfetti, LargeConfetti, ZigZag, Wave, DiagonalBrick, HorizontalBrick, Weave, Plaid, Divot, DottedGrid, DottedDiamond, Shingle, Trellis, Sphere, SmallGrid, SmallCheckerBoard, LargeCheckerBoard, OutlinedDiamond, SolidDiamond, LargeGrid, Min, Max
int length = 6;// Number of characters to generate
//*** END Editable Values
int height = 100;
int width = 200;
width = width + ((length - 6) * 30);
Random ranRotate = new Random();
string strText = System.Guid.NewGuid().ToString();
strText = strText.Replace("-", String.Empty);
strText = strText.Substring(0, length);
Bitmap bmpCanvas = new Bitmap(width, height, PixelFormat.Format24bppRgb);
Graphics graCanvas = Graphics.FromImage(bmpCanvas);
RectangleF recF = new RectangleF(0, 0, width, height);
Brush bruBackground = default(Brush);
SolidBrush letterBrush = default(SolidBrush);
graCanvas.TextRenderingHint = TextRenderingHint.AntiAlias;
if (RandomBackgroundNoiseColor == true) {
bruBackground = new HatchBrush(BackgroundNoiseTexture, Color.FromArgb((ranRotate.Next(0, 255)), (ranRotate.Next(0, 255)), (ranRotate.Next(0, 255))), Color.FromArgb(BackgroundColor[0], BackgroundColor[1], BackgroundColor[2]));
}
else {
bruBackground = new HatchBrush(BackgroundNoiseTexture, Color.FromArgb(BackgroundNoiseColor[0], BackgroundNoiseColor[1], BackgroundNoiseColor[2]), Color.FromArgb(BackgroundColor[0], BackgroundColor[1], BackgroundColor[2]));
}
graCanvas.FillRectangle(bruBackground, recF);
if (RandomTextColor == true) {
letterBrush = new SolidBrush(Color.FromArgb((ranRotate.Next(0, 255)), (ranRotate.Next(0, 255)), (ranRotate.Next(0, 255))));
}
else {
letterBrush = new SolidBrush(Color.FromArgb(TextColor[0], TextColor[1], TextColor[2]));
}
System.Drawing.Drawing2D.Matrix matRotate = new System.Drawing.Drawing2D.Matrix();
int i = 0;
for (i = 0; i <= strText.Length - 1; i++) {
matRotate.Reset();
int intChars = strText.Length;
int x = width / (intChars + 1) * i;
int y = height / 2;
//matRotate.RotateAt(ranRotate.Next(-30, 30), new PointF(width / (intChars + 1) * i, height * 0.5) );
matRotate.RotateAt(ranRotate.Next(-30, 30), new PointF(x, y) );
graCanvas.Transform = matRotate;
if (i == 0) {
//draw ‘the text on our image
//graCanvas.DrawString(strText.Chars(i), new Font("Comic Sans MS", 25, FontStyle.Italic), letterBrush, width / (intChars + 1) * i, height * 0.4);
graCanvas.DrawString(strText.Substring(i, 1), new Font("Comic Sans MS", 25, FontStyle.Italic), letterBrush, width / (intChars + 1) * i, 40);
}
else if (i == 1) {
//draw ‘the text on our image
graCanvas.DrawString(strText.Substring(i, 1), new Font("Arial", 30, FontStyle.Bold), letterBrush, width / (intChars + 1) * i, 10);
}
else if (i == 2) {
//draw ‘the text on our image
graCanvas.DrawString(strText.Substring(i, 1), new Font("Times New Roman", 25, FontStyle.Italic), letterBrush, width / (intChars + 1) * i, 40);
}
else if (i == 3) {
//draw ‘the text on our image
graCanvas.DrawString(strText.Substring(i, 1), new Font("Georgia", 35, FontStyle.Bold), letterBrush, width / (intChars + 1) * i, 10);
}
else if (i == 4) {
//draw ‘the text on our image
graCanvas.DrawString(strText.Substring(i, 1), new Font("Verdana", 25, FontStyle.Italic), letterBrush, width / (intChars + 1) * i, 40);
}
else if (i == 5) {
//draw ‘the text on our image
graCanvas.DrawString(strText.Substring(i, 1), new Font("Geneva", 30, FontStyle.Bold), letterBrush, width / (intChars + 1) * i, 10);
}
else {
//draw ‘the text on our image
graCanvas.DrawString(strText.Substring(i, 1), new Font("Verdana", 25, FontStyle.Italic), letterBrush, width / (intChars + 1) * i, 40);
}
graCanvas.ResetTransform();
}
bmpCanvas.Save(path, ImageFormat.Gif);
graCanvas.Dispose();
bmpCanvas.Dispose();
return strText;
}
public void subCheckCaptcha(object sender, System.EventArgs e)
{
if (!(txtCaptcha.Text == Session["strText"].ToString())) {
lblMessage.Text = "The characters does not match";
}
else {
lblMessage.Text = "";
subSubmitForm();
}
}
public void subSubmitForm()
{
//Your code in here
Response.Write("Captcha text entered successfully");
}
Use the following controls in your form section to display the Captcha to your website users