VB.NET - Simple Captcha Application


SUBMITTED BY: Guest

DATE: Nov. 15, 2013, 11:11 p.m.

FORMAT: Text only

SIZE: 3.5 kB

HITS: 1376

  1. This Simple Captcha Application doesn't need Internet to load it's Captcha but it generates a random image for you to answer.
  2. Picture: http://cur.lv/4q76m
  3. Okay, you need the following elements:
  4. 2 Button
  5. 1 PictureBox
  6. 1 TextBox
  7. Button 1 will be the Submit button. Checking if the answer is correct or incorrect.
  8. Button 2 will be the one refreshing a new generated image for you to answer.
  9. TextBox you need this to answer lmao;
  10. PictureBox you need this to show the image lmao;
  11. Okay, let's start. Add this code below your Public Class.
  12. This code will create the question and the lines so it's a bit harder to read which makes it a bit challenging.
  13. Private cAnswer As String = Nothing
  14. Private Function genQuestion() As String
  15. Dim cOperators As String() = {"+", "-"}
  16. Start:
  17. Dim p1 As Integer = New Random().Next(1, 9)
  18. Dim p2 As Integer = New Random().Next(1, 9)
  19. If p1 = p2 Then GoTo Start
  20. Dim cOperator As String = cOperators(New Random().Next(0, cOperators.Length))
  21. Select Case cOperator
  22. Case "+"
  23. cAnswer = p1 + p2
  24. If cAnswer <= 0 Then GoTo Start
  25. Case "-"
  26. cAnswer = p1 - p2
  27. If cAnswer <= 0 Then GoTo Start
  28. End Select
  29. Return String.Format("{0}{1}{2}", p1, cOperator, p2)
  30. End Function
  31. Private Sub generateLines(ByVal G As Graphics)
  32. If Not G Is Nothing Then
  33. Dim R As New Random()
  34. Dim lineBrush As New SolidBrush(Color.LightGray)
  35. For i% = 0 To 9
  36. G.DrawLines(New Pen(lineBrush, R.Next(1, 2)), New Point() {New Point(0, R.Next(0, 60)), New Point(200, R.Next(0, 60))})
  37. Next
  38. End If
  39. End Sub
  40. Private Function generateImage() As Image
  41. Dim B As New Bitmap(200, 60)
  42. Using G As Graphics = Graphics.FromImage(B)
  43. With G
  44. .Clear(Color.White)
  45. .DrawString(genQuestion(), New Font("Segoe UI", 20), Brushes.Black, New Rectangle(0, 0, 200, 60), New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
  46. End With
  47. generateLines(G)
  48. End Using
  49. Return B
  50. End Function
  51. So next, we will be coding the submit button or Button 1, this will submit your answer.
  52. Select Case TextBox1.Text
  53. Case Is = cAnswer
  54. MessageBox.Show("Correct!", "Correct!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
  55. PictureBox1.Image = generateImage()
  56. TextBox1.Clear()
  57. Case Else
  58. MessageBox.Show("Incorrect!", "Incorrect!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
  59. PictureBox1.Image = generateImage()
  60. TextBox1.Clear()
  61. End Select
  62. TextBox1.Clear()
  63. Button 2 and Form_Load will be the same code. Add this line of code to Button 2 and Form_Load.
  64. PictureBox1.Image = generateImage()
  65. When you are done, debug it and test. Thanks for reading and testing this thread, I'm pretty sure you've learned something new.
  66. Support here: http://forum.nsst.co/threads/egyszer%C5%B1-captcha-alkalmaz%C3%A1s-simple-captcha-application.19/

comments powered by Disqus