Create Captcha in Vb .Net  

Posted by Kunal Pradhan

Make a New Form
Make 1 Button, rename it as cmdDraw
Make 1 TextBox, rename it as txtSource
Make 1 PictureBox, rename it as picCaptcha(select the picture box
then go to properties, there will name

type the name there)

Now double click d form and remove all d text from the Coding part, if anything is der, just remove it

Now put d Code which is in dis page der:
Imports System.Drawing.Drawing2D
Imports System.Math

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents picCaptcha As System.Windows.Forms.PictureBox
Friend WithEvents txtSource As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents cmdDraw As System.Windows.Forms.Button

Private Sub InitializeComponent()
Me.picCaptcha = New System.Windows.Forms.PictureBox
Me.txtSource = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.cmdDraw = New System.Windows.Forms.Button
CType(Me.picCaptcha, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'picCaptcha
'
Me.picCaptcha.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.picCaptcha.Location = New System.Drawing.Point(32, 40)
Me.picCaptcha.Name = "picCaptcha"
Me.picCaptcha.Size = New System.Drawing.Size(192, 64)
Me.picCaptcha.TabIndex = 0
Me.picCaptcha.TabStop = False
'
'txtSource
'
Me.txtSource.Location = New System.Drawing.Point(40, 8)
Me.txtSource.Name = "txtSource"
Me.txtSource.Size = New System.Drawing.Size(120, 20)
Me.txtSource.TabIndex = 1
Me.txtSource.Text = "Visual Basic"
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(28, 13)
Me.Label1.TabIndex = 2
Me.Label1.Text = "Text"
'
'cmdDraw
'
Me.cmdDraw.Location = New System.Drawing.Point(176, 8)
Me.cmdDraw.Name = "cmdDraw"
Me.cmdDraw.Size = New System.Drawing.Size(64, 24)
Me.cmdDraw.TabIndex = 3
Me.cmdDraw.Text = "Draw"
'
'Form1
'
Me.AcceptButton = Me.cmdDraw
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(248, 109)
Me.Controls.Add(Me.cmdDraw)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.txtSource)
Me.Controls.Add(Me.picCaptcha)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.picCaptcha, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub

#End Region

' Make a captcha image for the text.
Private Function MakeCaptchaImge(ByVal txt As String, ByVal min_size As Integer, ByVal max_size As Integer, ByVal wid As Integer, ByVal hgt As Integer) As Bitmap
' Make the bitmap and associated Graphics object.
Dim bm As New Bitmap(wid, hgt)
Dim gr As Graphics = Graphics.FromImage(bm)
gr.SmoothingMode = Drawing2D.SmoothingMode.HighQuality

Dim rectf As New RectangleF(0, 0, wid, hgt)
gr.FillRectangle(Brushes.White, rectf)

' See how much room is available for each character.
Dim ch_wid As Integer = wid \ txt.Length

' Draw each character.
Dim rnd As New Random
For i As Integer = 0 To txt.Length - 1
Dim font_size As Single = rnd.Next(min_size, max_size)
Dim the_font As New Font("Arial", font_size, FontStyle.Bold)
DrawCharacter(txt.Substring(i, 1), gr, the_font, i * ch_wid, ch_wid, wid, hgt)
the_font.Dispose()
Next i

' Mess things up a bit.
Dim x As Integer = rnd.Next(16)
Do While x < wid
gr.DrawLine(Pens.Blue, x, 0, x, hgt)
x += 8 + rnd.Next(16)
Loop
Dim y As Integer = rnd.Next(16)
Do While y < hgt
gr.DrawLine(Pens.Blue, 0, y, wid, y)
y += 8 + rnd.Next(16)
Loop
For i As Integer = 1 To 20
Dim x1 As Integer = rnd.Next(wid)
Dim y1 As Integer = rnd.Next(hgt)
Dim x2 As Integer = rnd.Next(wid)
Dim y2 As Integer = rnd.Next(hgt)
gr.DrawLine(Pens.White, x1, y1, x2, y2)
Next i

gr.Dispose()

Return bm
End Function

' Draw a deformed character at this position.
Private Sub DrawCharacter(ByVal txt As String, ByVal gr As Graphics, ByVal the_font As Font, ByVal X As Integer, ByVal ch_wid As Integer, ByVal wid As Integer, ByVal hgt As Integer)
' Center the text.
Dim string_format As New StringFormat
string_format.Alignment = StringAlignment.Center
string_format.LineAlignment = StringAlignment.Center
Dim rectf As New RectangleF(X, 0, ch_wid, hgt)

' Convert the text into a path.
Dim graphics_path As New GraphicsPath
graphics_path.AddString(txt, the_font.FontFamily, _
CInt(Font.Style), the_font.Size, rectf, string_format)

' Make random warping parameters.
Dim rnd As New Random
Dim x1 As Single = CSng(X + rnd.Next(ch_wid) / 2)
Dim y1 As Single = CSng(rnd.Next(hgt) / 2)
Dim x2 As Single = CSng(X + ch_wid / 2 + rnd.Next(ch_wid) / 2)
Dim y2 As Single = CSng(hgt / 2 + rnd.Next(hgt) / 2)
Dim pts() As PointF = { _
New PointF(CSng(X + rnd.Next(ch_wid) / 4), CSng(rnd.Next(hgt) / 4)), _
New PointF(CSng(X + ch_wid - rnd.Next(ch_wid) / 4), CSng(rnd.Next(hgt) / 4)), _
New PointF(CSng(X + rnd.Next(ch_wid) / 4), CSng(hgt - rnd.Next(hgt) / 4)), _
New PointF(CSng(X + ch_wid - rnd.Next(ch_wid) / 4), CSng(hgt - rnd.Next(hgt) / 4)) _
}
Dim mat As New Matrix
graphics_path.Warp(pts, rectf, mat, WarpMode.Perspective, 0)

' Rotate a bit randomly.
Dim dx As Single = CSng(X + ch_wid / 2)
Dim dy As Single = CSng(hgt / 2)
gr.TranslateTransform(-dx, -dy, MatrixOrder.Append)
Static prev_angle As Integer = 0
Dim angle As Integer = prev_angle
Do While Abs(angle - prev_angle) < 30
angle = rnd.Next(-60, 60)
Loop
prev_angle = angle
Debug.WriteLine(angle)
gr.RotateTransform(angle, MatrixOrder.Append)
gr.TranslateTransform(dx, dy, MatrixOrder.Append)

' Draw the text.
gr.FillPath(Brushes.Blue, graphics_path)
gr.ResetTransform()
graphics_path.Dispose()
End Sub

Private Sub cmdDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDraw.Click
Dim txt As String = txtSource.Text
Dim bm As Bitmap = MakeCaptchaImge(txt, _
22, 30, _
picCaptcha.ClientSize.Width, _
picCaptcha.ClientSize.Height)
picCaptcha.Image = bm
End Sub

This entry was posted on Sunday, June 22, 2008 and is filed under , , , . You can leave a response and follow any responses to this entry through the Subscribe to: Post Comments (Atom) .

0 comments