To reduce the incidence of spam and
reduce the likelihood of denial of service attacks, many web pages include an
image block with distorted text, known as a Captcha.
Captcha is an image with a code written on it. The website visitor is required
to read the code on the image and enter the value in a text field. If the word
entered is wrong, the form submission is not processed. As CAPTCHA is a smartly
blurred image, the spam bot can't read it. So the form cannot be auto-submitted
by a 'bot'.
This article will give step by step tutorial how to create Capcta in ASP.Net
using c#.
STEP 1
Create Dynamic Images as per the Image size.
private void GenerateImage()
{
// Create a new 32-bit bitmap image.
Bitmap bitmap = new Bitmap(this.m_width,
this.m_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.m_width, this.m_height);
// Fill
in the background.
HatchBrush hatchBrush = new
HatchBrush(HatchStyle.SmallConfetti,
Color.LightGray, Color.White);
g.FillRectangle(hatchBrush, rect);
// Set
up the text font.
SizeF size = default(SizeF);
float fontSize = rect.Height + 1;
Font font = default(Font);
// Adjust the font size until the text
fits within the image.
do
{
fontSize -= 1;
font = new Font(this.familyName,
fontSize, FontStyle.Bold);
size = g.MeasureString(this.m_text,
font);
} while (size.Width > rect.Width);
// Set
up the text format.
StringFormat format = new
StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;


Vinod KumarPosted Mar 2, 2015, 4:06 AM
code is not working at all
anvesh anuPosted Nov 18, 2013, 5:48 AM
nice article biswa.. still am getting problem while executing this above program
AShish RawatPosted Mar 31, 2012, 1:32 PM
nice code , but still it is difficult to understand ,so could you post in simpler way...
Mohammad MirshahiPosted Sep 7, 2011, 2:00 AM
very good... where do you live? are you cantact me? pls visit my site: www.mfc-project.blogfa.com