Results 1 to 6 of 6

Tutorial to Create Captcha in Vb .Net

This is a discussion on Tutorial to Create Captcha in Vb .Net within the Programming forums, part of the Web Designing & Development category; Make a New Form Make 1 Button, rename it as cmdDraw Make 1 TextBox, rename it as txtSource Make 1 ...

  1. #1
    Senior Member Array sagar4evr's Avatar
    Join Date
    Feb 2008
    Posts
    354

    Default Tutorial to Create Captcha in Vb .Net

    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 the form and remove all the text from the Coding part, if anything is there, just remove it

    Now put the Code which is in this page:

    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).BeginIni t()
    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

  2. #2
    Junior Member Array Madinfo's Avatar
    Join Date
    Oct 2008
    Posts
    25

    Default Re: Tutorial to Create Captcha in Vb .Net

    Cool! Thanks for sharing.

  3. #3
    Senior Member Array Rayz.Mario's Avatar
    Join Date
    May 2008
    Posts
    735

    Default Re: Tutorial to Create Captcha in Vb .Net

    Very well explained.

  4. #4
    Senior Member Array msn90's Avatar
    Join Date
    May 2009
    Location
    india
    Posts
    146

    Thumbs up Re: Tutorial to Create Captcha in Vb .Net

    This is very helpful for everyone. great job, keep it up

  5. #5
    Junior Member Array
    Join Date
    Nov 2009
    Posts
    4

    Default Re: Tutorial to Create Captcha in Vb .Net

    Hi..
    Thanks for sharing the information... Its really good one...

  6. #6
    Junior Member Array
    Join Date
    Aug 2010
    Posts
    4

    Default Re: Tutorial to Create Captcha in Vb .Net

    Well Explaned. Good job thanks for sharing it.



Similar Threads

  1. How to create a sitemap
    By SticKer in forum Advertising & Promotion
    Replies: 12
    Last Post: 04-13-2009, 10:56 AM
  2. Can some one link me to a tutorial or reference site for doing simple 2d gfx in C#?
    By [email protected] in forum Graphic Design
    Replies: 1
    Last Post: 12-10-2008, 07:15 AM
  3. Replies: 5
    Last Post: 11-27-2008, 06:37 AM
  4. Stumbleupon adds the captcha code
    By SticKer in forum Member Discussion
    Replies: 8
    Last Post: 03-20-2008, 05:04 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO Forum | Web Hosting Forum | Websites For Sale |