Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim My_Stream As MemoryStream = Get_Images(12, 4, "#ffffff") '参数分别为:字体大小,验证码位数,背景色
Show_image(My_Stream) '显示内存图像
My_Stream.Close() '关闭打开的流文件
End Sub
'将字符转换成图像
'参数说明:
' Font_Size 字体大小
' Char_Number 验证码位数
' BackgroundColor 背景色
Function Get_Images(ByVal Font_Size As Integer, ByVal Char_Number As Integer, ByVal BackgroundColor As String) As MemoryStream
Dim image_w As Integer = Int(Font_Size * 1.5) + Font_Size * Char_Number
Dim image_h As Integer = Int(Font_Size / 2)
Dim Sesson_Company As String = "" '为了进行验证比较
Dim n As Integer
For n = 0 To Char_Number - 1
Dim Show_Str As String = getChar() '要显示为图像的字符
Sesson_Company = Sesson_Company & Show_Str
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) '定义文本格式(字体,字号,粗体)
Temp_Graphics.DrawString(Show_Str, Show_Str_Font, New SolidBrush(Color_Font), Int(Font_Size / 2) + n * Font_Size, Int(image_h * 0.125 * Rnd() + image_h * 0.08)) '绘出字符 '绘字符的Y方向下波动+4
Next
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)
Response.ClearContent()
Response.ContentType = "Image/Jpeg"
Response.BinaryWrite(Show_Stream.ToArray())
Response.End()
End Function
'获得随机颜色
Function getColor() As String
Dim int_a As Integer
Dim int_b As Integer
Dim int_c As Integer
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)
Return "#" & Hex(int_a) & Hex(int_b) & Hex(int_c)
End Function
'获得随机字体
Function getFont() As String
Dim font_array(7)
font_array(0) = "Arial Black"
font_array(1) = "BatangChe"
font_array(2) = "Century"
font_array(3) = "Arial Black"
font_array(4) = "Arial Black"
font_array(5) = "Arial Black"
font_array(6) = "Arial Black"
Return font_array(Int(7 * Rnd()))
End Function
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{