Important alert: (current site time 7/15/2013 7:28:08 AM EDT)
 

article

Signup using Image Verification Tutorial

Email
Submitted on: 6/20/2005 1:34:53 PM
By: rajat talwar 
Level: Beginner
User Rating: By 38 Users
Compatibility: VB.NET, ASP.NET
Views: 37535
 
     All Major Websites dealing with secure content, administrative rights and user privacy use image verification. an image is provided with a random string embossed on it and before registering the string has to be entered in the provided textbox. This can be easily seen at Yahoo. COM, gmail, planet source code (yes our very own PSC will ask you for this when you vote for me).


 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				





Imports System


Signup using Image Verification Tutorial

All Major Websites dealing with secure content, administrative rights and user privacy use image verification. an image is provided with a random string embossed on it and before registering the string has to be entered in the provided textbox. This can be easily seen at Yahoo. COM, gmail, planet source code (yes our very own PSC will ask you for this when you vote for me).

Lets try and work at producing such a random image so that it can be easily embedded in any of professional website. I prefer working with classes so let’s develop a verification class.

First we will import the required namespaces.

Imports System.Drawing.Text 'for font

Imports System.Drawing.Imaging 'for saving the gif

Imports System.Security.Cryptography 'for creating random String

Public Class Verification

 

Public Function CreateImage(ByVal path As String, ByVal height As Integer, ByVal width

As Integer) As String

'Creates a Random Gif file of provided width and height

'the string on the gif file is rotated randomly

'returns the random string painted

Dim r As New Random 'to generate a random angle

Dim salt As String = CreateSalt(4) 'generates a random string

Dim bmp As New Bitmap(width, height, PixelFormat.Format24bppRgb) 'creates a

24bit bitmap in memory

Dim g As Graphics = Graphics.FromImage(bmp)

g.TextRenderingHint = TextRenderingHint.AntiAlias 'this will smoothen the Font

g.Clear(Color.Black) 'this clears the background and paints specified color

g.DrawRectangle(Pens.White, 1, 1, width - 3, height - 3)

g.DrawRectangle(Pens.Black, 0, 0, width, height)

Dim mymat As New System.Drawing.Drawing2D.Matrix 'matrix used for rotation

transformation

Dim i As Integer

For i = 0 To Len(salt) - 1 'we will rotate each literal at a specified angle

mymat.Reset()    ' matrix should be initialized to identity matrix

mymat.RotateAt(r.Next(-30, 0), New PointF(width * (0.12 * i), height * 0.5))

'rotate at any angle b/w -30 and 0

g.Transform = mymat 'apply the transform

g.DrawString(salt.Chars(i), New Font("Comic Sans MS", 10, FontStyle.Italic),

SystemBrushes.ActiveCaptionText, width * (0.12 * i), height * 0.5) 'draw the text on

our image

g.ResetTransform()

Next

bmp.Save(path, ImageFormat.Gif) 'save the gif at specified path and name

g.Dispose() 'clean up

bmp.Dispose() 'ok the mess is over

Return salt 'return the string painted for verification

End Function

 

Public Function CreateSalt(ByVal size As Integer) As String

' Generate a cryptographic random number using the cryptographic

' service provider

Dim rng As New RNGCryptoServiceProvider

Dim buff(size) As Byte

rng.GetBytes(buff)

' Return a Base64 string representation of the random number

Return Convert.ToBase64String(buff)

End Function

End Class

 

Here I have used a single font. You may use different fonts for each literal. This can be easily done by storing different font family names in an array

Now let’s get on with “How to use the class”. Drop an image box, label, textbox and a button from the toolbox to an aspx page use image box ‘id=image height=100, width=200; button id=btnRegister; textbox id=textbox1; label id=lblMessage

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

Handles MyBase.Load

'Put user code to initialize the page here

If Not Page.IsPostBack Then    'we do not want picture to be created when page is postback

'postback happens when asp.net fires it's control's event

Dim verify As New Verification

Dim salt As String = verify.CreateImage(Server.MapPath(".\Random.gif"), 100,

200)    'I have created a file named Random.gif in the same directory as my web page size 100,200

Image.ImageUrl = Server.MapPath(".\Random.gif")

Session.Add("salt", salt)    'Add our salt to session for verification so that we can check on postback

Dim params As System.Collections.Specialized.NameValueCollection

params = Request.QueryString()

If params.Count > 0 Then lblMessage.Text = params.Item("reason")

End If

End Sub

 

Private Sub btnRegister_Click(ByVal sender As System.Object, ByVal e As System.

EventArgs) Handles btnRegister.Click

If Not TextBox1.Text.Equals(Session.Item("salt")) Then

Response.Redirect(Request.Url.ToString & "?reason=The Strings did not match")    'if we donot do it page will be considered a postback

Else

lblMessage.text=”Good Boy”

End If

End Sub

 

I have used sessions here. U can also use encrypted cookies or encrypted url passing.

Keep in mind size of proportion b/w size of your image box and size of image you are creating. This affects your image’s clarity and also font may go out of bounds

Plz vote for me and you can request working demo at my email id

 

 

 


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

6/22/2005 4:37:19 AMFabrizio

Very nice, but what happens if, say 20 users connect together? Will the "Random.gif" image be overwritten 20 times and are we sure the 20 users will see each one its "own" image?
(If this comment was disrespectful, please report it.)

 
6/22/2005 1:38:04 PMrajat talwar

Thankx Nice ques Fabrizio
For those sites with a good amount of visitors i use a random name every time for my file using the same create salt function pass it as a session element in page load event and delete the file in btnregister_click event. cumbersome but very effective!
Thankx i missed this point
(If this comment was disrespectful, please report it.)

 
6/27/2005 1:06:52 AM

Thanks for a wonderful tutorial on this. I deployed it on two sites I am running with minor changes. However, there is one site that I am using it with that is behind an SSL. Somehow, I cannot access the Random.gif using server.mappath. I set write permissions on the directory, but still, it shows as a broken image. Any ideas?
(If this comment was disrespectful, please report it.)

 
6/28/2005 2:19:18 PMrajat talwar

an SSL does prevent anything on the server to be accessed. u can do onething if u have already deployed the website u can get a static path for the image and store it as an application key to use it
anyways u will face this problem throughout the building your website use of server elements is common

(If this comment was disrespectful, please report it.)

 
6/28/2005 2:21:10 PMrajat talwar

an SSL does prevent anything on the server to be accessed. u can do onething if u have already deployed the website u can get a static path for the image and store it as an application key to use it
anyways u will face this problem throughout the building your website use of server elements is common
just check u have all the files imported
for the web form
server element is obviously not available in anywhere else
(If this comment was disrespectful, please report it.)

 
6/30/2005 5:03:25 AM

The code is superb.
(If this comment was disrespectful, please report it.)

 
8/13/2005 3:41:45 AM

this article is marvelous
(If this comment was disrespectful, please report it.)

 
8/20/2005 7:38:50 PMRodney

It must work i get an error GDI+ generic error. Any ideas.
"A generic error occurred in GDI+."
(If this comment was disrespectful, please report it.)

 
8/20/2005 7:41:47 PMRodney

Sorry wrong permission on the folder works great.........
(If this comment was disrespectful, please report it.)

 
12/6/2005 12:06:16 PMtriggerman

awesome code. One thing you could consider is to set the image src to a method in your page where you actually return gif data (binarywrite). Random image names is actually going to give you best performance though. Nice job.
(If this comment was disrespectful, please report it.)

 
5/15/2006 5:14:40 AMErika

Your code is very useful: I'm trying to convert it into C#.
Could you send me the demo?
Many thanks

(If this comment was disrespectful, please report it.)

 
8/31/2006 5:31:01 AMErika

I'm succeded to convert the image verification code to C#.
Now I'd like to share it.
Could you tell me how could I publish the code on this website?
Many thanks for all.

(If this comment was disrespectful, please report it.)

 
9/1/2006 12:48:10 AMrajat talwar

Good Work Erika! When you sinin there is an Upload link at the bottom. The code is good for .Net 1.1 but there is a new captcha (image verification) built in .NET 2. Try to look at that too
(If this comment was disrespectful, please report it.)

 
12/20/2006 6:47:13 AMrani

a conflict will come when multiple users came
(If this comment was disrespectful, please report it.)

 
4/7/2007 6:24:53 AMjimmasters

i receive this error "'Next' is not a member of 'WebAppplication1.random'" from this line, Hope anyone could help!

mymat.RotateAt(r.Next(-30, 0), New PointF(width * (0.12 * i), height * 0.5))

(If this comment was disrespectful, please report it.)

 
10/15/2007 3:07:43 AMKantha Moorthy

good start for the begineer...
(If this comment was disrespectful, please report it.)

 
10/22/2007 12:34:46 PMbaysoft_dan

Hi there. We have created a free ASP.NET verification image control, that is not dissimilar to the one that is seen on Google. You can download it or try it here http://www.roundedpanel.baysoft-net.co.uk/Default.aspx
(If this comment was disrespectful, please report it.)

 
11/26/2007 4:03:33 AMLakshmi Narayana

Great Work for Begineers
(If this comment was disrespectful, please report it.)

 
11/27/2007 2:02:35 PMT3rM1ght

Thanks a lot, this is a nice release. I used it to build a VB.NET version of this, and bypassed the need to save to a file. I also parse out the | + = signs in the salt string (quite annoying >.<) and I am thinking of using a bit smaller, but text ONLY random string generation method to build that salt, as well as randomizing the font for each character when rendering. Good stuff man :)
(If this comment was disrespectful, please report it.)

 
12/18/2007 11:30:26 PMyung

Dim verify As New Verification

Verification cannot be defined?
why?
(If this comment was disrespectful, please report it.)

 
12/19/2007 2:01:55 AMyung

Dim verify As New Verification

Verification cannot be defined?
how define it?
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.