生成变形的文字

Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO


  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim My_Stream As MemoryStream = Get_Images(20, 5, "#E0E0E0") '参数分别为:字体大小,验证码位数,背景色
        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)

        '******************************************
        ' 保存图像的 宽度 和 高度,供调用页面使用
        '******************************************
        Session("image_w") = image_w
        Session("image_h") = image_h


        Dim Temp_Bitmap As Bitmap '封装GDI+位图
        Dim Temp_Graphics As Graphics '封装GDI+绘图面
        Dim Color_Back As Color = ColorTranslator.FromHtml(BackgroundColor) '背景颜色

        Temp_Bitmap = New Bitmap(image_w, 5 * image_h, PixelFormat.Format32bppRgb) '确定背景大小

        Temp_Graphics = Graphics.FromImage(Temp_Bitmap)
        Temp_Graphics.FillRectangle(New SolidBrush(Color_Back), New Rectangle(0, 0, image_w, 5 * image_h)) '绘制背景

        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

        '********************************
        ' 保存到session便于调用的页面比较
        '********************************
        Session("imagenumber") = 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)
        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)
        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
        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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值