By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,822 Members | 729 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,822 IT Pros & Developers. It's quick & easy.

CAPTCHA image .NET error/problem

P: 8
Hi.

I have this CAPTCHA image for .Net in C-Sharp and translate it into VB and as Code_Behind.
http://www.mbarrick.com/blog/d6plinks/20061221-01

If i run the site/script i get the site but there is not being showing an CAPTCHA image, Why not !?
http://www.kennelenggaard.dk/captcha.aspx

Hope someone can help me with this.

My code on Default.aspx have this line:
Expand|Select|Wrap|Line Numbers
  1. <img src="TuringImage.aspx" alt="" />
  2.  

My code on TuringImages.aspx have this code:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="TuringImage.aspx.vb" Inherits="TuringImage" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5. <title>TurningImage</title>
  6. </head>
  7. <body>
  8. </body>
  9. </html>
  10.  

My code_behind for this site it then:
Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2. Imports System.Collections
  3. Imports System.ComponentModel
  4. Imports System.Data
  5. Imports System.Drawing
  6. Imports System.Drawing.Drawing2D
  7. Imports System.Drawing.Imaging
  8. Imports System.Web
  9. Imports System.Web.SessionState
  10. Imports System.Web.UI
  11. Imports System.Web.UI.WebControls
  12. Imports System.Web.UI.HtmlControls
  13.  
  14. Partial
  15. Class TuringImage
  16. Inherits System.Web.UI.Page
  17. Private random As Random = New Random
  18. Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
  19. 'Width and height of image
  20. Dim width As Integer = 200
  21. Dim height As Integer = 50
  22. Dim familyName As String = "Times New Roman"
  23. 'String imageText = "123456";
  24. Dim imageText As String = Me.Session("TuringImageText").ToString
  25. 'Create a new 32-bit bitmap image.
  26. Dim bitmap As System.Drawing.Image = New Bitmap(width, height, PixelFormat.Format32bppArgb)
  27. 'Create a graphics object for drawing.
  28. Dim g As Graphics = Graphics.FromImage(bitmap)
  29. g.SmoothingMode = SmoothingMode.AntiAlias
  30. Dim rect As Rectangle = New Rectangle(0, 0, width, height)
  31. 'Fill in the background.
  32. Dim hatchBrush As HatchBrush = New HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White)
  33. g.FillRectangle(hatchBrush, rect)
  34. 'Set up the text font.
  35. Dim size As SizeF
  36. Dim fontSize As Single = rect.Height + 1
  37. Dim font As Font
  38. 'Adjust the font size until the text fits within the image.
  39. Do
  40. System.Math.Max(System.Threading.Interlocked.Decrement(CLng(fontSize)), fontSize + 1)
  41. font = New Font(familyName, fontSize, FontStyle.Bold)
  42. size = g.MeasureString(imageText, font)
  43. Loop While size.Width > rect.Width
  44. 'Set up the text format.
  45. Dim format As StringFormat = New StringFormat
  46. format.Alignment = StringAlignment.Center
  47. format.LineAlignment = StringAlignment.Center
  48. 'Create a path using the text and warp it randomly.
  49. Dim path As GraphicsPath = New GraphicsPath
  50. path.AddString(imageText, font.FontFamily, CType(font.Style, Integer), font.Size, rect, format)
  51. Dim v As Single = 4.0F
  52. Dim points As PointF() = {New PointF(Me.random.Next(rect.Width) / v, Me.random.Next(rect.Height) / v), New PointF(rect.Width - Me.random.Next(rect.Width) / v, Me.random.Next(rect.Height) / v), New PointF(Me.random.Next(rect.Width) / v, rect.Height - Me.random.Next(rect.Height) / v), New PointF(rect.Width - Me.random.Next(rect.Width) / v, rect.Height - Me.random.Nex(rect.Height) / v)}
  53. Dim matrix As Matrix = New Matrix
  54. matrix.Translate(0.0F, 0.0F)
  55. path.Warp(points, rect, matrix, WarpMode.Perspective, 0.0F)
  56. 'Draw the text.
  57. hatchBrush = New HatchBrush(HatchStyle.LargeConfetti, Color.LightGray, Color.DarkGray)
  58. g.FillPath(hatchBrush, path)
  59. 'Add some random noise.
  60. Dim m As Integer = Math.Max(rect.Width, rect.Height)
  61. Dim i As Integer = 0
  62. While i < CType((rect.Width * rect.Height / 30.0F), Integer)
  63. Dim x As Integer = Me.random.Next(rect.Width)
  64. Dim y As Integer = Me.random.Next(rect.Height)
  65. Dim w As Integer = Me.random.Next(m / 50)
  66. Dim h As Integer = Me.random.Next(m / 50)
  67. g.FillEllipse(hatchBrush, x, y, w, h)
  68. System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
  69. End While
  70. 'Change the response headers to output a JPEG image.
  71. Me.Response.Clear()
  72. Me.Response.ContentType = "image/jpeg"
  73. bitmap.Save(Me.Response.OutputStream, ImageFormat.Jpeg)
  74. 'Clean up.
  75. 'font.Dispose();
  76. hatchBrush.Dispose()
  77. g.Dispose()
  78. End Sub
  79. End Class 
  80.  
Mar 26 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.