Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
CAPTCHA in asp.net
Author:
Arindam Ghosh
E-mail:
Click to e-mail author
Website:
http://www.studentsstudy.com
Submitted:
8/10/2013
Version:
ASP.NET 3.5
Compatibility:
ASP.NET 3.5, ASP.NET 4.0
Category:
ASP.NET
Views:
7769
How to create a simple CAPTCHA using Asp.net and with C#.
Declarations:
'none
Code:
The Code is very simple .For writing Code you need to import following namespace: using System.Drawing; using System.Drawing.Text; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.IO; using System.Web.SessionState; then the code is : public partial class _Default : System.Web.UI.Page { public static int round= 62; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Show_Captha(); } } public void Show_Captha() { Bitmap ObjBitmap= new Bitmap(160,50); Graphics objGraphics =Graphics.FromImage(ObjBitmap); objGraphics.SmoothingMode = SmoothingMode.AntiAlias; objGraphics.Clear(Color.Gray); objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias; Font objFont = new Font("AR CENA", 12, System.Drawing.FontStyle.Bold); string strCaptcha = string.Empty; strCaptcha = Return_Capchastring(); objGraphics.DrawString(strCaptcha, objFont, Brushes.White, 3, 3); ObjBitmap.Save(Server.MapPath("Image").ToString() +"/capcha.png"); Image1.ImageUrl = "Image/capcha.png"; } public string Return_Capchastring() { string []Arr=new string[]{"0","1","2","3","4","5","6","7","8","9", "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", "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" }; string Fcapcha = ""; string Scapcha = ""; Random R=new Random(); for (int i = 0; i < 8; i++) { Fcapcha = Fcapcha + Arr[R.Next(round)].ToString(); } Scapcha = Fcapcha.Substring(3, 4); Fcapcha = Fcapcha.Substring(0, 4); Session["capthacode"] = Fcapcha + Scapcha; return " " +Fcapcha +"\n " +Scapcha; } protected void btnMatch_Click(object sender, EventArgs e) { if (Session["capthacode"].ToString() != txtYourText.Text) { Response.Write("Wrong Capcha"); } else { Response.Write("Accepted"); } } }
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2023 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement