How to create captcha image without dll file in asp.net

By
Captcha image is very helpful for any website.It prevent the brute force attacks on the websites.It is one part of secuity  features on the website.There are some other feature of security also that prevent the attacks on the websites.I will discuss it in any Next tutorials . Previously i have created  captcha image on the asp.net website with the help of .dll file.But now i am creating a simple randam captcha image without use of .dll file.Both captcaha generation method is fine.You can follow any method both are secured. If you want to make captcha image for our websites or others then follow the below steps which are given below:When you use this captcha application on your website then it will Work definitely .
There are some steps follow one by one which are given below:
Step 1:-  First open your visual studio 2010-->File-->.New-->Website-->select ASP.NET Empty website-->OK-->Now go solution Explorer and Add a Web Form-->And Drag and drop Label ,Text Boxes ,image and button control on the form as shown below:

design


Step 2:- Now open Solution Explorer --> Add New Captcha Folder-->Add New web Form(captcha.aspx) inside the Captcha Folder as shown below:
solution explorer

Step 3:- Now Double click on captcha.aspx page--> write the following codes on the page load as given below:-

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class captcha_captcha : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Bitmap bmap = new Bitmap(118,50);
        Graphics grfs = Graphics.FromImage(bmap);
        grfs.Clear(Color.White);
        Random rnd = new Random();
        grfs.DrawLine(Pens.Black, rnd.Next(0, 50), rnd.Next(10, 30), rnd.Next(0, 200), rnd.Next(0, 50));
        grfs.DrawRectangle(Pens.Blue, rnd.Next(0, 20), rnd.Next(0, 20), rnd.Next(50, 80), rnd.Next(0, 20));
        grfs.DrawLine(Pens.Red, rnd.Next(0, 50), rnd.Next(10, 30), rnd.Next(0, 200), rnd.Next(0, 20));
        string str = string.Format("{0:X}", rnd.Next(100000, 999999));
        Session["verify"] = str.ToLower();
        Font fnt = new Font("Arial", 20, FontStyle.Bold);
        grfs.DrawString(str, fnt, Brushes.Red, 30,20);
       // grfs.DrawString(text.Substring(0,3),fnt, Brushes.Navy, 20, 20);
        //grfs.DrawString(text.Substring(3),fnt, Brushes.Navy, 50, 20);
        bmap.Save(Response.OutputStream, ImageFormat.Gif);     
    }
}

Note:- First include Namespace using system.Drawing.Imaging as shown in the codes.
Step 4:- Now go First web Form (design form)-->Go properties of image control -->Click ImageUrl--> Select your catcha web form(captcha.aspx) from the captcha Folder as shown below-->OK
import image

Step 5:- Now Add a Database.mdf in Solution Explorer -->Create a table(student) as shown below:-
database
Note:- If you are getting any problem to add .mdf database on the website then follow the below links.
Step 6:- Now Double click on Submit button --> and Write the following codes as given below:-

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=.;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
        con.Open();
 String str = "INSERT INTO student(name,mobile,location) VALUES ('" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "')";
        SqlCommand cmd = new SqlCommand(str, con);
        cmd.ExecuteNonQuery();
        con.Close();
        if (TextBox1.Text.ToLower() == Session["verify"].ToString())
        {
            Response.Redirect("output.aspx");
        }
        else
            Label2.Text = "Please ReEnter captcha code";
    }
}

Step 7:- Now go Solution Explorer --> Add a New web Form for showing the output (information) after submitting the web page as shown below:-
msdotnet

Step 8:- Now Run the Application(press F5) --> you will see following output.

Step 9:- Now Enter the Required field after Run the Application --> Click Submit Button.


Step 10:- If you will Enter the Wrong captcha image codes then it will show followings Errors and captcha image codes will change as shown below:-

Note:- If you will follow Each steps then definitely,you will generate captcha image.If you are facing some problems then download this Application from the Bottom and run it directly on your visual studio 2010.But you can face sql server connection problems.follow the below link if any database problems
For More:-
  1. File Handling in c#
  2. Collections in c#
  3. Host ASP.NET Website on IIS Server
  4. Host ASP.NET Website on Server Free
  5. oops concepts in c#
  6. Transaction in SQL Server Database
  7. Windows Authentication in asp.net
  8. How to connect database through OoleDb
  9. How to use wcf services in asp.net application
  10. How to use session state in asp.net application
  11. Views in ms sql server
  12. Reflection in c#
  13. How to use secure login page in asp.net with example
  14. How to Add and verify Captcha image in 3 tier architecture in asp.net
  15. How to use virtual keyboard in asp.net for security purpose
  16. How to implement hashing concepts in asp.net
  17. Learn complete .Net Interview Questions and answers for job seekers
To Get the Latest  Free Updates Subscribe
 Download Whole Captcha Application
         Download

2 comments:

  1. SIR its not wrkng showing that bitmapdata does not contain constructor that takes two orguments..and also when i import namespace bitmapdata is coming instead of bitmap....please help me sir....

    ReplyDelete
    Replies
    1. For bitmap you have to need one name space >using system.Drawing.Imaging .create a fresh application in visual studio 2010 or other.follow each steps very care fully .it will work definitely. if you are facing problem again then you can send your code on ramashanker@outlook.com.i will correct it.
      You can use another way to create captcha image for your asp.net application, which are below:-
      http://www.msdotnet.co.in/2012/08/how-to-add-captcha-image-in-aspnet.html

      Delete

Powered by Blogger.