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/ .
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.