Pages

Wednesday, 26 February 2014

How to Create Captcha with Refresh Button in C#, VB.NET

How to Create Captcha with Refresh Button in C#, VB.NET 

.Aspx File
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Captcha Image with Refresh Button</title>
<script type="text/javascript">
    function RefreshCaptcha() {
        var img = document.getElementById("imgCaptcha");
        img.src = "Handler.ashx?query=" + Math.random();
    }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="Handler.ashx" id="imgCaptcha" />
<a href="#" onclick="javascript:RefreshCaptcha();">Refresh</a>
</div>
</form>
</body>
</html>

Now Right click on website
 -> Select Add New Item
 -> Select Generic Handler file and give name as Handler.ashx and click ok

open Handler.ashx
 
c#

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

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;

public class Handler : IHttpHandler {

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();
}
}
}
public bool IsReusable {
get {
return false;
}
}
}

VB


 <%@ WebHandler Language="VB" Class="Handler2" %>
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Web

Public Class Handler2 : Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Using b As New Bitmap(150, 40, PixelFormat.Format32bppArgb)
Using g As Graphics = Graphics.FromImage(b)
Dim rect As New Rectangle(0, 0, 149, 39)
g.FillRectangle(Brushes.White, rect)

' Create string to draw.
Dim r As New Random()
Dim startIndex As Integer = r.[Next](1, 5)
Dim length As Integer = r.[Next](5, 10)
Dim drawString As [String] = Guid.NewGuid().ToString().Replace("-", "0").Substring(startIndex, length)

' Create font and brush.
Dim drawFont As New Font("Arial", 16, FontStyle.Italic Or FontStyle.Strikeout)
Using drawBrush As New SolidBrush(Color.Black)
' Create point for upper-left corner of drawing.
Dim drawPoint As New PointF(15, 10)

' Draw string to screen.
g.DrawRectangle(New Pen(Color.Red, 0), rect)
g.DrawString(drawString, drawFont, drawBrush, drawPoint)
End Using
b.Save(context.Response.OutputStream, ImageFormat.Jpeg)
context.Response.ContentType = "image/jpeg"
context.Response.[End]()
End Using
End Using
End Sub

Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property

End Class

Demo




No comments:

Post a Comment