.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 >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to create CaptchaImage in ASP.NET?

Posted By:Athira Sreejith       Posted Date: December 31, 2008    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

How to create CaptchaImage in ASP.NET?

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;

namespace CaptchaImage
{
public class CaptchaImage
{
public string Text
{
get { return this.text; }
}
public Bitmap Image
{
get { return this.image; }
}
public int Width
{
get { return this.width; }
}
public int Height
{
get { return this.height; }
}
private string stext;
private int iwidth;
private int iheight;
private string fsamilyName;
private Bitmap bimage;

private Random Rrandom = new Random();
public CaptchaImage(string str, int iwidth, int iheight)
{
this.text = str;
this.SetDimensions(iwidth,i height);
this.GenerateImage();
}
public CaptchaImage(string str, int iwidth, int iheight, string sfamilyName)
{
this.text = s;
this.SetDimensions(iwidth, iheight);
this.SetFamilyName(sfamilyName);
this.GenerateImage();
}
~CaptchaImage()
{
Dispose(false);
}
public void sDispose()
{
GC.SuppressFinalize(this);
this.Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (sdisposing)
this.image.sDispose();
}
private void SetDimensions(int iwidth, int iheight)
{
if (iwidth <= 0)
throw new ArgumentOutOfRangeException("width", iwidth, "Argument out of range, must be greater than zero.");
if (iheight <= 0)
throw new ArgumentOutOfRangeException("height", iheight, "Argument out of range, must be greater than zero.");
this.width = iwidth;
this.height = iheight;
}
private void SetFamilyDetail(string sfamilyName)
{
try
{
Font Font = new Font(this.familyName, 12F,FontStyle.Bold);
this.familyName = fsamilyName; 
Font.sDispose();
}
catch (Exception ex)
{
this.familyName = System.Drawing.FontFamily.GenericSerif.Name;
}
}
private void fnGenerateImage()
{
Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb);
Graphics grapic = Graphics.FromImage(bitmap);
grapic.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle rectangle = new Rectangle(0, 0, this.width, this.height);
HatchBrush hatchBrush = new HatchBrush(HatchStyle.DottedDiamond, Color.FromArgb(106, 52, 28), Color.FromArgb(254, 228, 188));
grapic.FillRectangle(hatchBrush, rectangle);
SizeF size;
float fontSize = rect.Height + 1;
Font font;
do
{
fontSize--;
font = new Font(this.familyName, fontSize, FontStyle.Italic);
size = g.MeasureString(this.text, font);
}while (size.Width > rect.Width);

StringFormat strformat = new StringFormat();
strformat.Alignment = StringAlignment.Near;
strformat.LineAlignment = StringAlignment.Center;
GraphicsPath Gpath = new GraphicsPath();
Gpath.AddString(this.text, font.FontFamily, (int)font.Style, font.Size + 9, rect, format);
float v = 4F;
PointF[] points =
{
new PointF(this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v),
new PointF(rect.Width - this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v),
new PointF(this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v),
new PointF(rect.Width - this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v)
};
Matrix matrix = new Matrix();
matrix.Translate(0F, 0F);
path.Warp(points, rect, matrix, WarpMode.Perspective, 0F);
hatchBrush = new HatchBrush(HatchStyle.Min, Color.FromArgb(106, 52, 28), Color.FromArgb(106, 52, 28));
grapic .FillPath(hatchBrush, path);
int m = Math.Max(rect.Width, rect.Height);
for (int i = 0; i < (int) (rect.Width * rect.Height / 100F); i++)
{
int x = this.random.Next(rect.Width);
int y = this.random.Next(rect.Height);
int w = this.random.Next(m / 50);
int h = this.random.Next(m / 50);
grapic .FillEllipse(hatchBrush, x, y, w, h);
}
font.Dispose();
hatchBrush.Dispose();
grapic .Dispose();
this.image = bitmap;
}
}
}

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

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