You have to create an .aspx file like CaptureImage.aspx, which .cs file will be like this,

protected void Page_Load(object sender, EventArgs e)
{
string Filepath = Server.MapPath(“~/Images/captcha.bmp”);
Bitmap bp = Bitmap.FromFile(Filepath) as Bitmap;

MemoryStream memStream = new MemoryStream();
Graphics g = Graphics.FromImage(bp);
g.SmoothingMode = SmoothingMode.AntiAlias;

int width = bp.Width;
int height = bp.Height;

string familyName = “Tahoma”;
string text = “”;

Random random = new Random();

for (int i = 0; i < 5; i++)
{
text += Convert.ToChar(random.Next(‘A’, ‘Z’)).ToString();
}

Rectangle rect = new Rectangle(5, 5, 168, 38);
Font font;
font = new Font(familyName, 32F, FontStyle.Bold);

StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
GraphicsPath path = new GraphicsPath();
path.AddString(text, font.FontFamily, (int)font.Style, font.Size, rect, format);

PointF[] points =
{
new PointF(rect.X – 10, rect.Y – 8),
new PointF(rect.X + rect.Width – 20, rect.Y + 4),
new PointF(rect.X – 8, rect.Y + rect.Height – 15),
new PointF(rect.X + rect.Width – 10, rect.Y + rect.Height + 4)
};

Matrix matrix = new Matrix();
matrix.Translate(0F, 0F);
path.Warp(points, rect, matrix, WarpMode.Perspective, 0F);

HatchBrush hatchBrush = new HatchBrush(
HatchStyle.LargeConfetti,
Color.Black,
Color.Black);
g.FillPath(hatchBrush, path);

Response.Clear();
Response.ContentType = “image/jpeg”;
bp.Save(memStream, ImageFormat.Jpeg);
memStream.WriteTo(Response.OutputStream);

Session[“CaptureText”] = text;

font.Dispose();
hatchBrush.Dispose();
g.Dispose();
bp.Dispose();
}

Use this page like this,
imgCapture.ImageUrl = “CaptureImage.aspx”;
on submit check -> Session[“CaptureText”].ToString()

Advertisements