生成随机验证码图片程序
ashx代码
Imports System
Imports System.Web
Imports System.Drawing
Imports System.IO
Imports System.Web.UI
Public Class login_validatecode : Implements IHttpHandler
#Region "验证码的长度(默认6个)"
Private m_codeLenght As Integer = 6
Public Property CodeLenght() As Integer
Get
Return m_codeLenght
End Get
Set(ByVal value As Integer)
m_codeLenght = value
End Set
End Property
#End Region
#Region "验证码的字体大小(默认13相素)"
Private m_fontSize As Integer = 13
Public Property FontSize() As Integer
Get
Return m_fontSize
End Get
Set(ByVal value As Integer)
m_fontSize = value
End Set
End Property
#End Region
#Region "验证码的字体样式(默认Roman)"
Private m_fontFamily As String = "Roman"
Public Property FontFamily() As String
Get
Return m_fontFamily
End Get
Set(ByVal value As String)
m_fontFamily = value
End Set
End Property
#End Region
#Region "是否输出燥点"
Private m_point As Boolean = True
Public Property Point() As Boolean
Get
Return m_point
End Get
Set(ByVal value As Boolean)
m_point = value
End Set
End Property
#End Region
#Region "输出燥点的颜色(默认海洋蓝)"
Private m_pointColor As Color = Color.LightSeaGreen
Public Property PointColor() As Color
Get
Return m_pointColor
End Get
Set(ByVal value As Color)
m_pointColor = value
End Set
End Property
#End Region
#Region "图片背景噪音线数量(默认5条)"
Private m_lineNumber As Integer = 5
Public Property LineNumber() As Integer
Get
Return m_lineNumber
End Get
Set(ByVal value As Integer)
m_lineNumber = value
End Set
End Property
#End Region
#Region "图片背景噪音线颜色(默认灰色)"
Private m_lineColor As Color = Color.LightGray
Public Property LineColor() As Color
Get
Return m_lineColor
End Get
Set(ByVal value As Color)
m_lineColor = value
End Set
End Property
#End Region
#Region "图片的背景颜色(默认白色)"
Private m_backgroundColor As Color = Color.White
Public Property BackgroundColor() As Color
Get
Return m_backgroundColor
End Get
Set(ByVal value As Color)
m_backgroundColor = value
End Set
End Property
#End Region
#Region "图片边框颜色(默认灰色)"
Private m_rectangleColor As Color = Color.LightGray
Public Property RectangleColor() As Color
Get
Return m_rectangleColor
End Get
Set(ByVal value As Color)
m_rectangleColor = value
End Set
End Property
#End Region
#Region "随即颜色数组"
Private m_colors As Color() = {Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, _
Color.DarkCyan, Color.Purple}
Public Property Colors() As Color()
Get
Return m_colors
End Get
Set(ByVal value As Color())
m_colors = value
End Set
End Property
#End Region
#Region "随即字符串"
Private m_randomCode As String = "2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z"
Public Property RandomCode() As String
Get
Return m_randomCode
End Get
Set(ByVal value As String)
m_randomCode = value
End Set
End Property
#End Region
#Region "生成验证码图片"
Public Function CreateImage(ByVal code As String) As Bitmap
'设置字体大小
Dim FSize As Integer = Me.FontSize
Dim FWidth As Integer = FSize ' FSize + 2
'设置图片大小
Dim ImgWidth As Integer = CInt((code.Length * FWidth)) + code.Length * 2 ' CInt((code.Length * FWidth)) + 4
Dim ImaHeight As Integer = FSize +6 ‘ FSize * 2
'创建图片和画笔
Dim iamge As New Bitmap(ImgWidth, ImaHeight)
Dim gh As Graphics = Graphics.FromImage(iamge)
gh.Clear(m_backgroundColor)
'绘制图片的背景噪音线
Dim rand As New Random()
For i As Integer = 1 To m_lineNumber
Dim x1 As Integer = rand.Next(ImgWidth)
Dim x2 As Integer = rand.Next(ImgWidth)
Dim y1 As Integer = rand.Next(ImaHeight)
Dim y2 As Integer = rand.Next(ImaHeight)
gh.DrawLine(New Pen(m_lineColor), x1, y1, x2, y2)
Next
'绘制图片边框线
gh.DrawRectangle(New Pen(m_rectangleColor, 1), 0, 0, ImgWidth - 1, (ImaHeight - 1))
'绘制图片的前景噪音点
If m_point Then
For i As Integer = 0 To m_codeLenght * 2 - 1
Dim x As Integer = rand.Next(ImgWidth)
Dim y As Integer = rand.Next(ImaHeight)
iamge.SetPixel(x, y, Color.FromArgb(rand.Next()))
Next
'Me.MakePoint(gh, ImgWidth, ImaHeight)
End If
'绘制字符串随即颜色
MakeFontColor(gh, code, ImaHeight, FSize)
Return iamge
End Function
#End Region
#Region "产生随即字符串"
''' <summary>
''' 返回产成的随即字符串
''' </summary>
''' <param name="codeLenght">字符串的长度</param>
''' <returns>返回产成的随即字符串</returns>
Public Function MakeCode(ByVal codeLenght As Integer) As String
Dim splitStr As String() = m_randomCode.Split(",")
Dim newCode As String = Nothing
Dim rand As New Random(CType(DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
For i As Integer = 0 To codeLenght - 1
newCode &= splitStr(rand.Next(0, splitStr.Length - 1))
Next
Return newCode
End Function
#End Region
#Region "产成字符串的随即颜色"
Public Sub MakeFontColor(ByVal g As Graphics, ByVal str As String, ByVal imgHeight As Integer, ByVal fontSize As Integer)
Dim b As Brush
Dim colorIndex As Integer
Dim rand As New Random()
Dim left As Integer = 0, top As Integer = 0, top1 As Integer = 1, top2 As Integer = 1
Dim n1 As Integer = imgHeight - fontSize - 4
Dim n2 As Integer = n1 / 4
top1 = n2
top2 = n2 * 2
For i As Integer = 0 To m_codeLenght - 1
colorIndex = rand.[Next](m_colors.Length - 1)
b = New System.Drawing.SolidBrush(m_colors(colorIndex))
If i Mod 2 = 1 Then
top = top2
Else
top = top1
End If
left = i * (fontSize)
Dim f As New Font(Me.FontFamily, Me.FontSize)
g.DrawString(str.Substring(i, 1), f, b, left, top)
Next
End Sub
#End Region
#Region "随即产生燥点"
''' <summary>
''' 随即产生燥点
''' </summary>
''' <param name="g">画笔</param>
''' <param name="imgWidth">绘制的宽度</param>
''' <param name="imgHeight">绘制的高度</param>
Public Sub MakePoint(ByVal g As Graphics, ByVal imgWidth As Integer, ByVal imgHeight As Integer)
Dim rand As New Random()
Dim p As New Pen(m_pointColor, 0)
For i As Integer = 0 To m_codeLenght * 2 - 1
Dim x As Integer = rand.Next(imgWidth)
Dim y As Integer = rand.Next(imgHeight)
g.DrawRectangle(p, x, y, 1, 1)
Next
End Sub
#End Region
#Region "将绘制的图片输出到页面"
Public Sub CreateImageOnPage(ByVal code As String, ByVal text As HttpContext)
Dim ms As New MemoryStream()
Dim img As Bitmap = CreateImage(code)
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
text.Response.ClearContent()
text.Response.ContentType = "image/Jpeg"
text.Response.BinaryWrite(ms.GetBuffer())
ms.Close()
ms = Nothing
img.Dispose()
img = Nothing
End Sub
#End Region
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim gvc As New login_validatecode()
gvc.CodeLenght = 4
gvc.FontSize = 13
gvc.Point = False
Dim code As String = gvc.MakeCode(gvc.CodeLenght) '获取随即字符串
HttpContext.Current.Response.Cookies.Add(New HttpCookie("BBS_LOGIN_VALIDATECODE", code))
gvc.CreateImageOnPage(code, context)
context.Response.End()
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
Imports System.Web
Imports System.Drawing
Imports System.IO
Imports System.Web.UI
Public Class login_validatecode : Implements IHttpHandler
#Region "验证码的长度(默认6个)"
Private m_codeLenght As Integer = 6
Public Property CodeLenght() As Integer
Get
Return m_codeLenght
End Get
Set(ByVal value As Integer)
m_codeLenght = value
End Set
End Property
#End Region
#Region "验证码的字体大小(默认13相素)"
Private m_fontSize As Integer = 13
Public Property FontSize() As Integer
Get
Return m_fontSize
End Get
Set(ByVal value As Integer)
m_fontSize = value
End Set
End Property
#End Region
#Region "验证码的字体样式(默认Roman)"
Private m_fontFamily As String = "Roman"
Public Property FontFamily() As String
Get
Return m_fontFamily
End Get
Set(ByVal value As String)
m_fontFamily = value
End Set
End Property
#End Region
#Region "是否输出燥点"
Private m_point As Boolean = True
Public Property Point() As Boolean
Get
Return m_point
End Get
Set(ByVal value As Boolean)
m_point = value
End Set
End Property
#End Region
#Region "输出燥点的颜色(默认海洋蓝)"
Private m_pointColor As Color = Color.LightSeaGreen
Public Property PointColor() As Color
Get
Return m_pointColor
End Get
Set(ByVal value As Color)
m_pointColor = value
End Set
End Property
#End Region
#Region "图片背景噪音线数量(默认5条)"
Private m_lineNumber As Integer = 5
Public Property LineNumber() As Integer
Get
Return m_lineNumber
End Get
Set(ByVal value As Integer)
m_lineNumber = value
End Set
End Property
#End Region
#Region "图片背景噪音线颜色(默认灰色)"
Private m_lineColor As Color = Color.LightGray
Public Property LineColor() As Color
Get
Return m_lineColor
End Get
Set(ByVal value As Color)
m_lineColor = value
End Set
End Property
#End Region
#Region "图片的背景颜色(默认白色)"
Private m_backgroundColor As Color = Color.White
Public Property BackgroundColor() As Color
Get
Return m_backgroundColor
End Get
Set(ByVal value As Color)
m_backgroundColor = value
End Set
End Property
#End Region
#Region "图片边框颜色(默认灰色)"
Private m_rectangleColor As Color = Color.LightGray
Public Property RectangleColor() As Color
Get
Return m_rectangleColor
End Get
Set(ByVal value As Color)
m_rectangleColor = value
End Set
End Property
#End Region
#Region "随即颜色数组"
Private m_colors As Color() = {Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, _
Color.DarkCyan, Color.Purple}
Public Property Colors() As Color()
Get
Return m_colors
End Get
Set(ByVal value As Color())
m_colors = value
End Set
End Property
#End Region
#Region "随即字符串"
Private m_randomCode As String = "2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z"
Public Property RandomCode() As String
Get
Return m_randomCode
End Get
Set(ByVal value As String)
m_randomCode = value
End Set
End Property
#End Region
#Region "生成验证码图片"
Public Function CreateImage(ByVal code As String) As Bitmap
'设置字体大小
Dim FSize As Integer = Me.FontSize
Dim FWidth As Integer = FSize ' FSize + 2
'设置图片大小
Dim ImgWidth As Integer = CInt((code.Length * FWidth)) + code.Length * 2 ' CInt((code.Length * FWidth)) + 4
Dim ImaHeight As Integer = FSize +6 ‘ FSize * 2
'创建图片和画笔
Dim iamge As New Bitmap(ImgWidth, ImaHeight)
Dim gh As Graphics = Graphics.FromImage(iamge)
gh.Clear(m_backgroundColor)
'绘制图片的背景噪音线
Dim rand As New Random()
For i As Integer = 1 To m_lineNumber
Dim x1 As Integer = rand.Next(ImgWidth)
Dim x2 As Integer = rand.Next(ImgWidth)
Dim y1 As Integer = rand.Next(ImaHeight)
Dim y2 As Integer = rand.Next(ImaHeight)
gh.DrawLine(New Pen(m_lineColor), x1, y1, x2, y2)
Next
'绘制图片边框线
gh.DrawRectangle(New Pen(m_rectangleColor, 1), 0, 0, ImgWidth - 1, (ImaHeight - 1))
'绘制图片的前景噪音点
If m_point Then
For i As Integer = 0 To m_codeLenght * 2 - 1
Dim x As Integer = rand.Next(ImgWidth)
Dim y As Integer = rand.Next(ImaHeight)
iamge.SetPixel(x, y, Color.FromArgb(rand.Next()))
Next
'Me.MakePoint(gh, ImgWidth, ImaHeight)
End If
'绘制字符串随即颜色
MakeFontColor(gh, code, ImaHeight, FSize)
Return iamge
End Function
#End Region
#Region "产生随即字符串"
''' <summary>
''' 返回产成的随即字符串
''' </summary>
''' <param name="codeLenght">字符串的长度</param>
''' <returns>返回产成的随即字符串</returns>
Public Function MakeCode(ByVal codeLenght As Integer) As String
Dim splitStr As String() = m_randomCode.Split(",")
Dim newCode As String = Nothing
Dim rand As New Random(CType(DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
For i As Integer = 0 To codeLenght - 1
newCode &= splitStr(rand.Next(0, splitStr.Length - 1))
Next
Return newCode
End Function
#End Region
#Region "产成字符串的随即颜色"
Public Sub MakeFontColor(ByVal g As Graphics, ByVal str As String, ByVal imgHeight As Integer, ByVal fontSize As Integer)
Dim b As Brush
Dim colorIndex As Integer
Dim rand As New Random()
Dim left As Integer = 0, top As Integer = 0, top1 As Integer = 1, top2 As Integer = 1
Dim n1 As Integer = imgHeight - fontSize - 4
Dim n2 As Integer = n1 / 4
top1 = n2
top2 = n2 * 2
For i As Integer = 0 To m_codeLenght - 1
colorIndex = rand.[Next](m_colors.Length - 1)
b = New System.Drawing.SolidBrush(m_colors(colorIndex))
If i Mod 2 = 1 Then
top = top2
Else
top = top1
End If
left = i * (fontSize)
Dim f As New Font(Me.FontFamily, Me.FontSize)
g.DrawString(str.Substring(i, 1), f, b, left, top)
Next
End Sub
#End Region
#Region "随即产生燥点"
''' <summary>
''' 随即产生燥点
''' </summary>
''' <param name="g">画笔</param>
''' <param name="imgWidth">绘制的宽度</param>
''' <param name="imgHeight">绘制的高度</param>
Public Sub MakePoint(ByVal g As Graphics, ByVal imgWidth As Integer, ByVal imgHeight As Integer)
Dim rand As New Random()
Dim p As New Pen(m_pointColor, 0)
For i As Integer = 0 To m_codeLenght * 2 - 1
Dim x As Integer = rand.Next(imgWidth)
Dim y As Integer = rand.Next(imgHeight)
g.DrawRectangle(p, x, y, 1, 1)
Next
End Sub
#End Region
#Region "将绘制的图片输出到页面"
Public Sub CreateImageOnPage(ByVal code As String, ByVal text As HttpContext)
Dim ms As New MemoryStream()
Dim img As Bitmap = CreateImage(code)
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
text.Response.ClearContent()
text.Response.ContentType = "image/Jpeg"
text.Response.BinaryWrite(ms.GetBuffer())
ms.Close()
ms = Nothing
img.Dispose()
img = Nothing
End Sub
#End Region
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim gvc As New login_validatecode()
gvc.CodeLenght = 4
gvc.FontSize = 13
gvc.Point = False
Dim code As String = gvc.MakeCode(gvc.CodeLenght) '获取随即字符串
HttpContext.Current.Response.Cookies.Add(New HttpCookie("BBS_LOGIN_VALIDATECODE", code))
gvc.CreateImageOnPage(code, context)
context.Response.End()
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class