<%@ WebHandler Language="VB" Class="captcha" %>
Imports System
Imports System.Web
Imports System.Drawing
Imports System.Drawing.Text
Imports System.IO
Imports System.Drawing.Drawing2D

Public Class captcha : Implements IHttpHandler, IRequiresSessionState
    
    Public _bgcolor As Color = (Color.FromArgb(77, 77, 77))      'Captcha's back-ground Color code
    Public _fontname As String = "Arial"                            'Captcha's font name
    Public _fontsize As String = "16"                               'Captcha's font size
    Public _fontcolor As Color = (Color.FromArgb(255, 255, 255))    'Captcha's font color
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Try
            Dim a As Integer = GetRandomNumber(10, 99)
            Dim b As Integer = GetRandomNumber(0, 9)
            System.Web.HttpContext.Current.Session("ASPCAPTCHA") = (a + b)
            Dim captcha As String = String.Format("{0} + {1} = ?", a, b)
            'Dim img As Bitmap = Convert_Text_to_Image(captcha, "Arial", 16)
            Dim img As Image = Convert_Text_to_Image(captcha)
            context.Response.Clear()
            context.Response.Buffer = True
            context.Response.ContentType = "image/png"
            context.Response.CacheControl = "no-cache"
            context.Response.AddHeader("Pragma", "no-cache")
            context.Response.Expires = -1
            'img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png)
            Using ms As New MemoryStream()
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
                ms.WriteTo(context.Response.OutputStream)
            End Using
        Catch ex As Exception
            context.Response.ContentType = "text/plain"
            context.Response.Write(ex.Message)
            context.Response.Write(ex.StackTrace)
        End Try
            
    End Sub

    Public Function GetRandomNumber(ByVal Min As Integer, ByVal Max As Integer) As Integer
        Dim Generator As System.Random = New System.Random()
        Return Generator.Next(Min, Max)
    End Function
        
    Public Function Convert_Text_to_Image(ByVal txt As String) As Bitmap
        'creating bitmap image
        Dim bmp As Bitmap = New Bitmap(1, 1)
        'FromImage method creates a new Graphics from the specified Image.
        Dim graphics As Graphics = graphics.FromImage(bmp)
        graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
        graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
        graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
        'Create the Font object for the image text drawing.
        Dim font As Font = New Font(_fontname, _fontsize)
        'Instantiating object of Bitmap image again with the correct size for the text and font.
        Dim stringSize As SizeF = graphics.MeasureString(txt, font)
        bmp = New Bitmap(bmp, 105, 32)
        graphics = graphics.FromImage(bmp)
        graphics.Clear(_bgcolor)
        'graphics.Clear(Color.White)
            
        'It can also be a way
        'bmp = new Bitmap(bmp, new Size((int)graphics.MeasureString(txt, font).Width, (int)graphics.MeasureString(txt, font).Height));*/

        'Draw Specified text with specified format 
        Dim textBrush As Brush = New SolidBrush(_fontcolor)
        graphics.DrawString(txt, font, textBrush, 1, 4)
        font.Dispose()
        graphics.Flush()
        graphics.Dispose()
        Return bmp     'return Bitmap Image 
    End Function
    
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
    
End Class~/rootfiles/13277/js/captcha.ashx