.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

how to create captcha image in asp.net

Posted By: Suma     Posted Date: March 11, 2009    Points:2   Category :ASP.Net
How to create Captcha Image containing alphanumeric value dynamically to prevent spam submission in asp.net. I am using c# as my language.

Please help me with code.

Thanks in advance
Suma

Responses
Author: susant             
Posted Date: March 13, 2009     Points: 5   

hi suma just try this..

Create Captcha
----------------------


protected void Page_Load(object sender, EventArgs e)
{
System.Drawing.Bitmap objBmp = new System.Drawing.Bitmap(90, 25);
System.Drawing.Graphics objGraphics = System.Drawing.Graphics.FromImage(objBmp);
objGraphics.Clear(System.Drawing.Color.Green);
objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
System.Drawing.Font objFont = new System.Drawing.Font("Acoustic Light", 11, System.Drawing.FontStyle.Bold);
string strRandom = "";
string[] strArray = new string[36];
strArray = new string[] { "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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; Random autoRand = new Random();
int x;
for (x = 0; x < 6; x++)
{
int i = Convert.ToInt32(autoRand.Next(0, 36));
strRandom += strArray[i].ToString();
}
Session.Add("strRandom", strRandom);
objGraphics.DrawString(strRandom, objFont, System.Drawing.Brushes.White, 3, 3);
Response.ContentType = "image/GIF";
objBmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
objFont.Dispose();
objGraphics.Dispose();
objBmp.Dispose();
}

susant
Mumbai

Author: susant             
Posted Date: March 13, 2009     Points: 5   

hi suma

If u r feeling difficulty then u can use guid to create alpha numeric captcha..like this..

string str=Guid.NewGuid();
string captcha=str.substring(0,5).tostring();

so asign the captch value to any lable and just make some font change of lanble...

susant
Mumbai..

Author: Athira Sreejith             
Posted Date: March 16, 2009     Points: 5   

Below is the sample code to generate captcha image in asp.net pragmatically to prevent spam.



cs:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Drawing.Imaging;

using System.Drawing;

using System.IO;

using System.Text;

public partial class captchageneration : System.Web.UI.Page

{

Random rand = new Random();

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

CreateImage();

}

}

public void CreateImage()

{

string code = GetRandomText();

Bitmap bitmap = new Bitmap(150, 50,

System.Drawing.Imaging.PixelFormat.Format32bppArgb);

Graphics g = Graphics.FromImage(bitmap);

Pen pen = new Pen(Color.Yellow);

Rectangle rect = new Rectangle(0, 0, 150, 50);

SolidBrush b = new SolidBrush(Color.Cornsilk);

SolidBrush blue = new SolidBrush(Color.Maroon);

int counter = 0;

g.DrawRectangle(pen, rect);

g.FillRectangle(b, rect);

for (int i = 0; i < code.Length; i++)

{

g.DrawString(code[i].ToString(),

new Font("Verdena", 10 + rand.Next(14, 18)),

blue, new PointF(10 + counter, 10));

counter += 20;

}

//DrawRandomLines(g);

Response.ContentType = "image/gif";

bitmap.Save(Response.OutputStream, ImageFormat.Gif);

g.Dispose();

bitmap.Dispose();

}

private string GetRandomText()

{

StringBuilder randomText = new StringBuilder();

if (Session["Code"] == null)

{

string alphabets = "abcdefghijklmnopqrstuvwxyz";

Random r = new Random();

for (int j = 0; j <= 5; j++)

{

randomText.Append(alphabets[r.Next(alphabets.Length)]);

}

Session["Code"] = randomText.ToString();

}

return Session["Code"] as String;

}

private void DrawRandomLines(Graphics g)

{

SolidBrush green = new SolidBrush(Color.Green);

for (int i = 0; i < 20; i++)

{

g.DrawLines(new Pen(green, 2), GetRandomPoints());

}

}

private Point[] GetRandomPoints()

{

Point[] points = { new Point(rand.Next(10, 150),

rand.Next(10, 150)),

new Point(rand.Next(10, 100),

rand.Next(10, 100)) };

return points;

}

}

Author: rkraju             
Posted Date: October 23, 2013     Points: 20   


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend