how to create a captcha image in asp.net c# using System.Drawing?

Posted by Prabu_Spark under ASP.NET on 9/12/2013 | Points: 10 | Views : 12142 | Status : [Member] | Replies : 10
Hi sir,
Tell me the steps to create a captcha image in asp.net c# using System.Drawing. I have a code to generate captcha text, but i need to display as image to the user instead of displaying in label or textbox. Kindly give me the solution for this problem.


[Hint: Need code to draw a text image using System.Drawing. Suppose if i pass the text as "Ky9f8" it will draw the corresponding image draw]

Kindly find the attachment below for further reference.

With regards,
J.Prabu.
[Email:prbspark@gmail.com]



Responses

Posted by: Bandi on: 9/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer these links
http://www.codeproject.com/Articles/99148/Simple-CAPTCHA-Create-your-own-in-C
http://developerdesk.blogspot.in/2012/11/to-generate-captcha-image-c.html#!/2012/11/to-generate-captcha-image-c.html
http://www.codeproject.com/Articles/169371/Captcha-Image-using-C-in-ASP-NET

For alphanumeric CAPTCHA
http://forums.asp.net/t/1871186.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
--Sample code
//Captcha.ashx.cs
using System;

using System.Web;
using System.Drawing;
using System.IO;
using System.Web.SessionState;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Drawing.Drawing2D;

namespace CAPTCHADrawing
{
/// <summary>
/// Summary description for Captcha
/// </summary>
public class Captcha : IHttpHandler, IReadOnlySessionState
{
public void ProcessRequest(HttpContext context)
{
int iHeight = 80;
int iWidth = 190;
Random oRandom = new Random();

int[] aBackgroundNoiseColor = new int[] { 150, 150, 150 };
int[] aTextColor = new int[] { 0, 0, 0 };
int[] aFontEmSizes = new int[] { 15, 20, 25, 30, 35 };

string[] aFontNames = new string[] { "Comic Sans MS", "Arial", "Times New Roman", "Georgia", "Verdana", "Geneva"};
FontStyle[] aFontStyles = new FontStyle[] { FontStyle.Bold, FontStyle.Italic, FontStyle.Regular, FontStyle.Strikeout, FontStyle.Underline};
HatchStyle[] aHatchStyles = new HatchStyle[]
{
HatchStyle.BackwardDiagonal, HatchStyle.Cross,
HatchStyle.DashedDownwardDiagonal, HatchStyle.DashedHorizontal,
HatchStyle.DashedUpwardDiagonal, HatchStyle.DashedVertical,
HatchStyle.DiagonalBrick, HatchStyle.DiagonalCross,
HatchStyle.Divot, HatchStyle.DottedDiamond, HatchStyle.DottedGrid,
HatchStyle.ForwardDiagonal, HatchStyle.Horizontal,
HatchStyle.HorizontalBrick, HatchStyle.LargeCheckerBoard,
HatchStyle.LargeConfetti, HatchStyle.LargeGrid,
HatchStyle.LightDownwardDiagonal, HatchStyle.LightHorizontal,
HatchStyle.LightUpwardDiagonal, HatchStyle.LightVertical,
HatchStyle.Max, HatchStyle.Min, HatchStyle.NarrowHorizontal,
HatchStyle.NarrowVertical, HatchStyle.OutlinedDiamond,
HatchStyle.Plaid, HatchStyle.Shingle, HatchStyle.SmallCheckerBoard,
HatchStyle.SmallConfetti, HatchStyle.SmallGrid,
HatchStyle.SolidDiamond, HatchStyle.Sphere, HatchStyle.Trellis,
HatchStyle.Vertical, HatchStyle.Wave, HatchStyle.Weave,
HatchStyle.WideDownwardDiagonal, HatchStyle.WideUpwardDiagonal, HatchStyle.ZigZag
};

//Get Captcha in Session
string sCaptchaText = context.Session["Captcha"].ToString();

//Creates an output Bitmap
Bitmap oOutputBitmap = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
Graphics oGraphics = Graphics.FromImage(oOutputBitmap);
oGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;

//Create a Drawing area
RectangleF oRectangleF = new RectangleF(0, 0, iWidth, iHeight);
Brush oBrush = default(Brush);

//Draw background (Lighter colors RGB 100 to 255)
oBrush = new HatchBrush(aHatchStyles[oRandom.Next
(aHatchStyles.Length - 1)], Color.FromArgb((oRandom.Next(100, 255)),
(oRandom.Next(100, 255)), (oRandom.Next(100, 255))), Color.White);
oGraphics.FillRectangle(oBrush, oRectangleF);

System.Drawing.Drawing2D.Matrix oMatrix = new System.Drawing.Drawing2D.Matrix();
int i = 0;
for (i = 0; i <= sCaptchaText.Length - 1; i++)
{
oMatrix.Reset();
int iChars = sCaptchaText.Length;
int x = iWidth / (iChars + 1) * i;
int y = iHeight / 2;

//Rotate text Random
oMatrix.RotateAt(oRandom.Next(-40, 40), new PointF(x, y));
oGraphics.Transform = oMatrix;

//Draw the letters with Random Font Type, Size and Color
oGraphics.DrawString
(
//Text
sCaptchaText.Substring(i, 1),
//Random Font Name and Style
new Font(aFontNames[oRandom.Next(aFontNames.Length - 1)],
aFontEmSizes[oRandom.Next(aFontEmSizes.Length - 1)],
aFontStyles[oRandom.Next(aFontStyles.Length - 1)]),
//Random Color (Darker colors RGB 0 to 100)
new SolidBrush(Color.FromArgb(oRandom.Next(0, 100),
oRandom.Next(0, 100), oRandom.Next(0, 100))),
x,
oRandom.Next(10, 40)
);
oGraphics.ResetTransform();
}

MemoryStream oMemoryStream = new MemoryStream();
oOutputBitmap.Save(oMemoryStream, System.Drawing.Imaging.ImageFormat.Png);
byte[] oBytes = oMemoryStream.GetBuffer();

oOutputBitmap.Dispose();
oMemoryStream.Close();

context.Response.BinaryWrite(oBytes);
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}


//CaptchaImage.aspx design
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CaptchaImage.aspx.cs" Inherits="CAPTCHADrawing.CaptchaImage" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="imgCaptcha" ImageUrl="Captcha.ashx" runat="server" />
<asp:TextBox runat="server" ID="txtCaptcha"/>
<asp:Button Text="Submit" runat="server" onclick="Unnamed1_Click" />
</div>
</form>
</body>
</html>


//CaptchaImage.aspx.cs
u

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 9/12/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
http://www.c-sharpcorner.com/uploadfile/satyapriyanayak/fghdfghdh/

If this post helps you mark it as answer
Thanks

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mrkraju999 on: 10/23/2013 [Member] Starter | Points: 25

Up
0
Down
Please go through the link...

http://dotnetlearners.com/blogs/view/6/Create-your-own-captcha-image-generator-in-aspnet-using-c-net.aspx
http://dotnetlearners.com/blogs/view/6/Create-your-own-captcha-image-generator-in-aspnet-using-c-net.aspx

http://www.dotnetlearners.com/tutorial/angularjs/17/angularjs-tutorial-introuduction-to-angularjs

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: hareeshkumarkr-16190 on: 10/24/2013 [Member] Starter | Points: 25

Up
0
Down


Please go through the link

http://www.aspdotnet-suresh.com/2013/09/create-captcha-with-refresh-button-in-aspnet.html

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Samirbhogayta on: 10/29/2013 [Member] Starter | Points: 25

Up
0
Down
hi.. please try this.

protected void Page_Load(object sender, System.EventArgs e)
{
//create object of Bitmap Class and set its width and height.
Bitmap objBMP = new Bitmap(180, 51);
//Create Graphics object and assign bitmap object to graphics' object.
Graphics objGraphics = Graphics.FromImage(objBMP);
objGraphics.Clear(Color.White);
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
Font objFont = new Font("arial", 30, FontStyle.Bold);
//genetating random 6 digit random number
string randomStr = GeneratePassword();
//set this random number in session
Session.Add("randomStr", randomStr);
Session.Add("randomStrCountry", randomStr);
objGraphics.DrawString(randomStr, objFont, Brushes.Black, 2, 2);
Response.ContentType = "image/GIF";
objBMP.Save(Response.OutputStream, ImageFormat.Gif);
objFont.Dispose();
objGraphics.Dispose();
objBMP.Dispose();
}

I hope, it helps..

SAMIR
Sr. Software Engineer

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Samirbhogayta on: 11/11/2013 [Member] Starter | Points: 25

Up
0
Down
please refer this article

http://forums.asp.net/t/1935123.aspx

SAMIR
Sr. Software Engineer

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Xiaoyuandlg on: 6/29/2014 [Member] Starter | Points: 25

Up
0
Down
Though I don't have much experience towards this kind of things, I believe this will offer you hints:

http://www.businessrefinery.com/products/barcode_net/net_barcode_generator.html

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Xiaoyuandlg on: 6/29/2014 [Member] Starter | Points: 25

Up
0
Down
Though I don't have much experience towards this kind of things, I believe this will offer you hints: http://www.businessrefinery.com/products/barcode_net/net_barcode_generator.html

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allensmith on: 1/27/2015 [Member] Starter | Points: 25

Up
0
Down
What is the best, most effective and yet easiest way of making image captcha security questions with ASP.net Netframework 4.0 ?
http://www.keepautomation.com/how_to/aspnet/csharp_barcode.html

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response