<%@ Import Namespace="system.Drawing"%>
<%@ Import Namespace="system.Drawing.Imaging"%>
<%@ Import Namespace="system.IO"%>

<script language="vb" runat="server">
    '验证码的值存放在session("captcha_int")中，不区分大小写！
'调用：<img src="验证码.aspx" alt=""/>

sub page_load(sender As Object, E As EventArgs)
 'Font_Size'字体大小 intrger
 'Char_Number'验证码的位数 intrger
 'BackgroundColor'背景颜色
 dim My_Stream as MemoryStream = Get_Images(12,50,"#EEEEEE")
 '显示内存图像
 Show_image(My_Stream)
 '关闭打开的流文件
 My_Stream.Close()
end sub

'Font_Size'字体大小intrger
'Char_Number'验证码的位数intrger
'BackgroundColor'背景颜色
'把字符转换为图像，并且保存到内存流
function Get_Images(Font_Size as integer,Char_Number as integer,BackgroundColor as string) as MemoryStream
 '这个数字在调用页面需要,你要自己算出明确的数值，分别为图片宽度和高度
        Dim image_w As Integer = 75
        Dim image_h As Integer = 22
 '封装GDI+位图
 dim Temp_Bitmap as Bitmap
 '封装GDI+绘图面
 dim Temp_Graphics as Graphics
 '背景颜色
 dim Color_Back as Color = ColorTranslator.FromHtml(BackgroundColor)
 '确定背景大小
        Temp_Bitmap = New Bitmap(image_w, image_h, PixelFormat.Format32bppRgb)
 Temp_Graphics = Graphics.FromImage(Temp_Bitmap)
 '绘制背景
        Temp_Graphics.FillRectangle(New SolidBrush(Color_Back), New Rectangle(0, 0, image_w, image_h))
 '为了进行验证比较
 DIM Sesson_Company AS String = ""
 dim n as integer
        For n = 0 To 4
            '要显示为图像的字符
            Dim Show_Str As String = getChar()
            Sesson_Company = Sesson_Company & Show_Str
            '字体随机大小，下波动2
            Dim Show_Str_Font_Size As Integer = Int(3 * Rnd() + (Font_Size - 2))
            '字体随机颜色
            Dim Color_Font As Color = ColorTranslator.FromHtml(getColor())
            '字体
            Dim Show_Font_Name As String = getFont()
            '定义文本格式（字体，字号,粗体）
            Dim Show_Str_Font As Font = New Font(Show_Font_Name, Show_Str_Font_Size, FontStyle.Bold)
            '绘出字符，绘字符的Y方向下波动＋4
            Temp_Graphics.DrawString(Show_Str, Show_Str_Font, New SolidBrush(Color_Font), Int(Font_Size / 2) + n * Font_Size, 0)
        Next
 '保存到session便于调用的页面比较,不区分大小写
        Session("captcha_int") = LCase(Trim(Sesson_Company))
 
 Font_Size = Font_Size * 4
 image_h = int(2.5 * Font_Size)

 dim Temp_Stream as MemoryStream = new MemoryStream()
 Temp_Bitmap.Save(Temp_Stream, ImageFormat.jpeg)
 '释放资源
 Temp_Graphics.Dispose()
 '释放资源
 Temp_Bitmap.Dispose()
 '关闭打开的流文件
 Temp_Stream.Close()
 '返回流
 return Temp_Stream
end function

'显示内存图像
    Function Show_image(ByVal Show_Stream As MemoryStream) As MemoryStream
        Response.ClearContent()
        Response.ContentType = "Image/Jpeg"
        Response.BinaryWrite(Show_Stream.ToArray())
        Response.End()
       
    End Function

'获得随机字符：0-9，a-z，A-Z
function getChar() as string
        Dim Char_array(5) As String
 randomize
 Char_array(0) = chr(Int(10 * Rnd() + 48))
 Char_array(1) = chr(Int(26 * Rnd() + 65))
 Char_array(2) = chr(Int(26 * Rnd() + 65))
 Char_array(3) = chr(Int(26 * Rnd() + 97))
 Char_array(4) = chr(Int(26 * Rnd() + 97))
 return Char_array(Int(5 * Rnd()))
end function

'获得随机颜色
function getColor() as string
 Dim int_a As integer
 Dim int_b As integer
 Dim int_c As integer
 randomize
 int_a = Int(180 * Rnd() + 20)
 int_b = Int(180 * Rnd() + 20)
 int_c = Int(180 * Rnd() + 20)
 if int_a > 150 and int_b > 150 and int_c > 150 then
  int_a = Int(150 * Rnd() + 20)
 end if
 return "#" & Hex(int_a) & Hex(int_b) & Hex(int_c)
end function

'获得随机字体
function getFont() as string
        Dim font_array(7) As String
 randomize
 font_array(0) = "Book Antiqua"
 font_array(1) = "Microsoft Sans Serif"
 font_array(2) = "Comic Sans MS"
 font_array(3) = "Comic Sans MS"
 font_array(4) = "Comic Sans MS"
 font_array(5) = "Comic Sans MS"
 font_array(6) = "Comic Sans MS"
 return font_array(Int(7 * Rnd()))
end function

'获得随机字体
function getFont1() as string
        Dim font_array(10) As String
 randomize
 font_array(0) = "华文行楷"
 font_array(1) = "隶书"
 font_array(2) = "华文彩云"
 font_array(3) = "方正舒体"
 font_array(4) = "华文彩云"
 font_array(5) = "方正舒体"
 font_array(6) = "华文彩云"
 font_array(7) = "方正舒体"
 font_array(8) = "楷体_GB2312"
 font_array(9) = "华文行楷"
 return font_array(Int(10 * Rnd()))
end function
</script>