[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

need for captcha codings

i need image captcha codings pls send it for me soon....
0
sunda2010
Asked:
sunda2010
  • 2
4 Solutions
 
liorfrCommented:
Here you go.

for the usage including storing the code in the session and retrieving please go to:
http://www.codeproject.com/KB/aspnet/CaptchaImage.aspx

Lior
0
 
liorfrCommented:
The create image function.
// ====================================================================

    // Creates the bitmap image.

    // ====================================================================

    private void GenerateImage()
    {
      // Create a new 32-bit bitmap image.

      Bitmap bitmap = new Bitmap(
        this.width,
        this.height,
        PixelFormat.Format32bppArgb);

      // Create a graphics object for drawing.

      Graphics g = Graphics.FromImage(bitmap);
      g.SmoothingMode = SmoothingMode.AntiAlias;
      Rectangle rect = new Rectangle(0, 0, this.width, this.height);

      // Fill in the background.

      HatchBrush hatchBrush = new HatchBrush(
        HatchStyle.SmallConfetti,
        Color.LightGray,
        Color.White);
      g.FillRectangle(hatchBrush, rect);

      // Set up the text font.

      SizeF size;
      float fontSize = rect.Height + 1;
      Font font;
      // Adjust the font size until the text fits within the image.

      do
      {
        fontSize--;
        font = new Font(
          this.familyName,
          fontSize,
          FontStyle.Bold);
        size = g.MeasureString(this.text, font);
      } while (size.Width > rect.Width);

      // Set up the text format.

      StringFormat format = new StringFormat();
      format.Alignment = StringAlignment.Center;
      format.LineAlignment = StringAlignment.Center;

      // Create a path using the text and warp it randomly.

      GraphicsPath path = new GraphicsPath();
      path.AddString(
        this.text,
        font.FontFamily,
        (int) font.Style,
        font.Size, 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);

      // Draw the text.

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

      // Add some random noise.

      int m = Math.Max(rect.Width, rect.Height);
      for (int i = 0; i < (int) (rect.Width * rect.Height / 30F); 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);
        g.FillEllipse(hatchBrush, x, y, w, h);
      }

      // Clean up.

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

      // Set the image.

      this.image = bitmap;
    }

Open in new window

0
 
CiTrusCommented:
Hi there,

I have experience that usage of your own CAPTCHA coda isn't as efficient as usage of some more widely-used CAPTCHA service. I have good experience with Google's reCAPTCHA: http://recaptcha.net/. And it also has API for .NET and others similar.

It works this way:
1. You register and get your API keys (just input your e-mail, nickname, password and target website)
2. You download DLL libraries with reCAPTCHA plugin and copy them to your Bin direcotry
3. You implement their code into your desired verification page like this:
   3.1 Just add this definition among headers: <%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>
   3.2 Then place reCAPTCHA control whenever you want it and fill in the API keys you got: <recaptcha:RecaptchaControl ID="recaptcha" runat="server" PublicKey="" PrivateKey="" />
   3.3 The reCAPTCHA control behaves like ordinary validator and can be checked by Page.IsVaild property

The complete guide can be found here: http://recaptcha.net/plugins/aspnet/
All other APIs can be found here: http://recaptcha.net/resources.html
Information why you should use this can be found here: http://recaptcha.net/learnmore.html

---------

If you don't want to use the reCAPTCHA free validation service, you can also create your own CAPTCHA image but on your own danger. Hereby I attach you the code I used before I found reCAPTCHA. This handler generates the verification code and stores it in the cookie called MyCaptcha (please notice that web browsers without cookie support will be boned).

Then you should put this into your validation page whenever you want the CAPTCHA image displayed (so far there can be only one CAPTCHA image per page): <img src="Captcha.ashx" />. You should also put some textbox in there to rewrite the verification code.

Now all the stuff is already in place and you just need to validate it. Place this line into part of your website code where you want to check the CAPTCHA:

bool captchaValid = GetMD5(CaptchaRewrite.Text) == Request.Cookies("MyCaptcha").Value;

You will have to copy the definition of GetMD5 function from the handler to the target page or make it globally accessible. You can of course change the cookie name by simple Search/Replace function... And the verification code rewrite textbox is called CaptchaRewrite in order for the line above to work (You can also rename it)...

Hope it helps,

Peter
// This code should be in the web handler called Captcha.ashx

<%@ WebHandler Language="C#" Class="Captcha" %>

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Security;
using System.Security.Cryptography;
public class Captcha : IHttpHandler
{
    // Here are the measures of desired image
    public int width = 170;
    public int height = 35;
    // Here is some basic font family name (it must be on the server)
    public string familyName = "Trebuchet MS";
    public string text;
    // Here are the color definitions in ARGB numbers (to fit in the target website)
    public Color background = Color.FromArgb(100, 221, 235, 179);
    public Color txtcolor = Color.FromArgb(100, 61, 71, 24);
    public Color foreground = Color.FromArgb(100, 187, 215, 102);
    public Random random = new Random();
    
    public void ProcessRequest(HttpContext context)
    {
        context.Response.AppendHeader("Cache-Control", "no-cache");
        context.Response.AppendHeader("Pragma", "no-cache");
        
        // Let's generate the verification code (betwenn 1000 and 100000) and store it in a cookie
        text = random.Next(1000, 100000).ToString();
        context.Response.SetCookie(new HttpCookie("MyCaptcha", GetMD5(text)));
        
        // Put the verification code into image with noise around
        Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb);
        Graphics g = Graphics.FromImage(bitmap);
        g.SmoothingMode = SmoothingMode.AntiAlias;
        Rectangle rect = new Rectangle(0, 0, this.width, this.height);
        
        HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, foreground, background);
        g.FillRectangle(hatchBrush, rect);
        
        SizeF size = default(SizeF);
        float fontSize = rect.Height + 1;
        Font font = default(Font);
        
        do {
            fontSize -= 1;
            font = new Font(this.familyName, fontSize, FontStyle.Bold);
            size = g.MeasureString(this.text, font);
        }
        while (size.Width > rect.Width);
        
        StringFormat format = new StringFormat();
        format.Alignment = StringAlignment.Center;
        format.LineAlignment = StringAlignment.Center;
        
        GraphicsPath path = new GraphicsPath();
        path.AddString(this.text, font.FontFamily, (int)font.Style, font.Size, 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.LargeConfetti, foreground, txtcolor);
        g.FillPath(hatchBrush, path);
        
        int m = Math.Max(rect.Width, rect.Height);
        for (int i = 0; i <= (int)(rect.Width * rect.Height / 30f) - 1; 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);
            g.FillEllipse(hatchBrush, x, y, w, h);
        }
        
        // Export our generated image into JPEG and return it in the output stream
        context.Response.ContentType = "image/jpeg";
        bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);
        
        font.Dispose();
        hatchBrush.Dispose();
        g.Dispose();
        bitmap.Dispose();
    }
    
    public bool IsReusable {
        get { return true; }
    }
    
    public string GetMD5(string input)
    {
        MD5 md5Hasher = MD5.Create();
        byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
        StringBuilder sBuilder = new StringBuilder();
        int i = 0;
        
        for (i = 0; i <= data.Length - 1; i++) {
            sBuilder.Append(data(i).ToString("x2"));
        }
        
        return sBuilder.ToString();
    }
    
}

Open in new window

0
 
Kumaraswamy RCommented:
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now