Showing posts with label Handler. Show all posts
Showing posts with label Handler. Show all posts

March 22, 2011

In the previous articles, I showed how to create the captcha control. In this post, I will write about the handler that generates the image based on the dynamic random text generated by the GetRandomText method of the CaptchaControl usercontrol.

To generate the image, the handler needs to send its output as an image type. To get random text, it needs to access the value from the Session. The handler uses the System.Drawing.Bitmap class to create the image. The width of the image is dynamically based on the number of characters of the random text. The handler uses the background image path property to set the background image and uses the font colour, font size and font family to write the text on the image. The RotateTransform method of the System.Drawing.Graphics class is used to slightly rotate the text on the image.  the yPos method is used to randomly position the characters. The output image is then send through the stream.

The code for the class is below and can also be downloaded from CodePlex at http://captchadotnet.codeplex.com/ .

CaptchaHandler
  1. using System;
  2. using System.Web;
  3. using System.Drawing;
  4.  
  5. namespace CaptchaApp
  6. {
  7.     /// <summary>
  8.     /// Summary description for CaptchaHandler
  9.     /// </summary>
  10.     public class CaptchaHandler : IHttpHandler, System.Web.SessionState.IReadOnlySessionState
  11.     {
  12.  
  13.         private Captcha cc;
  14.  
  15.         public void ProcessRequest(HttpContext context)
  16.         {
  17.             context.Response.ContentType = "image/jpeg";
  18.  
  19.             if (context.Session != null)
  20.             {
  21.                 if (context.Session["CaptchaClass"] != null)
  22.                 {
  23.                     cc = (Captcha)context.Session["CaptchaClass"];
  24.                 }
  25.                 else
  26.                 {
  27.                     cc = new Captcha();
  28.                 }
  29.             }           
  30.  
  31.             string text = string.Empty;
  32.             if(context.Session["captcha"] !=null)
  33.                 text = (string)context.Session["captcha"];
  34.  
  35.             int imageWidth = Convert.ToInt32(((cc.FontSize + 8) * text.Length));
  36.             int imageHeight = Convert.ToInt32(cc.FontSize * 2.5);
  37.  
  38.  
  39.             Bitmap bmp = new Bitmap(imageWidth, imageHeight);
  40.             var Grph = Graphics.FromImage(bmp);
  41.             Grph.FillRectangle(new SolidBrush(Color.Lavender), 0, 0, bmp.Width, bmp.Height);
  42.  
  43.             var grp = Graphics.FromImage(bmp);
  44.             Image background = Image.FromFile(HttpContext.Current.Server.MapPath(cc.BackgroundImagePath));
  45.             grp.DrawImage(background, new Rectangle(0, 0, bmp.Width, bmp.Height));
  46.  
  47.  
  48.             Grph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  49.             Grph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  50.             int xPos = 10;
  51.  
  52.             Font f = cc.GetFont();
  53.  
  54.             char[] textArray = text.ToCharArray();
  55.             int yPosition = 0;
  56.             Random r = new Random();
  57.  
  58.             for (int i = 0; i < textArray.Length; i++)
  59.             {
  60.                 if (i % 2 == 0)
  61.                     Grph.RotateTransform(5);
  62.                 else
  63.                     Grph.RotateTransform(-5);
  64.  
  65.                 yPosition = (int)(r.NextDouble() * 10);
  66.  
  67.                 Grph.DrawString(textArray[i].ToString(), f, new SolidBrush(Color.FromName(cc.TextColor)), xPos, yPosition);
  68.                 xPos += 20;
  69.  
  70.             }
  71.             
  72.             bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
  73.  
  74.         }
  75.  
  76.         public bool IsReusable
  77.         {
  78.             get
  79.             {
  80.                 return false;
  81.             }
  82.         }
  83.  
  84.  
  85.         private int yPos()
  86.         {
  87.             Random r = new Random();
  88.             return (int)(r.NextDouble() * 10);
  89.         }                
  90.     }
  91. }

The handler also implements the System.Web.SessionState.IReadOnlySessionState interface beside the IHttpHandler. This is necessary so that the handler can access the Session of the current context.

Reference: Shahed Kazi at AspNetify.com