生成图象验证码函数

类别:.NET开发 点击:0 评论:0 推荐:

    '生成图象验证码函数

    Sub ValidateCode(ByVal VNum As String)

        Dim Img As System.Drawing.Bitmap

        Dim g As Graphics

        Dim ms As System.IO.MemoryStream

        'gheight为图片宽度,根据字符长度自动更改图片宽度

        Dim gheight As Integer = Int(Len(VNum) * 11.5)

        '创建一个宽度已定,高度为20的图像

        Img = New Bitmap(gheight, 20)

        g = Graphics.FromImage(Img)

        '在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)

        g.DrawString(VNum, (New Font("宋体", 12)), (New SolidBrush(Color.Blue)), 3, 3)

        ms = New System.IO.MemoryStream

        Img.Save(ms, System.Drawing.Imaging.ImageFormat.Png)

        Response.ClearContent() '需要输出图象信息 要修改HTTP头

        Response.ContentType = "image/Png"

        Response.BinaryWrite(ms.ToArray())

        g.Dispose()

        Img.Dispose()

        Response.End()

    End Sub

本文地址:http://com.8s8s.com/it/it43896.htm