featured Create Captcha with refresh button (ASP.net- C#) Share on Facebook Share on Twitter Share on Google Plus 1:39 AM Bel-technology 0 Comments Create Captcha with refresh button (ASP.net- C#) We are providing you a simplest way to create captcha for your registration form. A refresh button is also there to change captcha. There is no need of any jquery in this code. Code explained here:- Create a generic handler file (Handler.ASHX) and add this code to ProcessRequest() method:- public void ProcessRequest (HttpContext context) { using (Bitmap b = new Bitmap(150, 40, PixelFormat.Format32bppArgb)) { using (Graphics g = Graphics.FromImage(b)) { Rectangle rect = new Rectangle(0, 0, 149, 39); g.FillRectangle(Brushes.White, rect); // Create string to draw. Random r = new Random(); int startIndex = r.Next(1, 5); int length = r.Next(5, 10); String drawString = Guid.NewGuid().ToString().Replace("-", "0").Substring(startIndex, length); // Create font and brush. Font drawFont = new Font("Arial", 16, FontStyle.Italic | FontStyle.Strikeout); using (SolidBrush drawBrush = new SolidBrush(Color.Black)) { // Create point for upper-left corner of drawing. PointF drawPoint = new PointF(15, 10); // Draw string to screen. g.DrawRectangle(new Pen(Color.Red, 0), rect); g.DrawString(drawString, drawFont, drawBrush, drawPoint); } b.Save(context.Response.OutputStream, ImageFormat.Jpeg); context.Response.ContentType = "image/jpeg"; context.Response.End(); } } } Code on the page where you want to show captcha:- <script type="text/javascript"> function RefreshCaptcha() { var img = document.getElementById("imgCaptcha"); img.src = "Handler.ashx?query=" + Math.random(); } </script> <form id="form1" runat="server"> <div> <img src="Handler.ashx" id="imgCaptcha" /> <a href="#" onclick="javascript:RefreshCaptcha();">Refresh</a> </div> </form> Download complete code Above code is functional. If you find any problem in applying this, please contact us. Visit our website www.bel-technology.com By Bel-technology You Might Also Like 0 comments
0 comments