| 用户组 : 上将 |
| 等级 : 商业用户 |
自封 : 无 |
| 发帖 : 24 |
| 积分 : 1054 |
| 金钱 : 1054 |
| 威望 : 1 |
| 注册 : 2006-8-18 2:11 |
|
|
第
1
楼
|
建立各code.aspx文件,把下面代码考入到code.vb文件中,然后建立个test.htm文件,在文件中写入
另外还需要一个字体样式文件font.TTF和code.aspx放到同一目录文件!font.TTF文件在附件中!
[code]
'program by someeyes
Imports System
Imports System.io
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Text
Public Class code
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
Private Sub InitializeComponent()
End Sub
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Response.Expires = 0
Response.ExpiresAbsolute = DateTime.Now
Response.AddHeader("pragma", "no-cache")
Response.CacheControl = "no-cache"
Dim CheckCode As String = RndNum(4)
Session("CheckCode") = CheckCode
ValidateCode(CheckCode)
End Sub
Sub ValidateCode(ByVal CheckCode)
Dim Img As System.Drawing.Bitmap
Dim g As Graphics
Dim ms As MemoryStream
Dim gwidth As Integer = Int(Len(CheckCode) * 12) + 4
Dim myColor(5) As Color
Dim WhiteBrush As New SolidBrush(Color.White)
Dim I As Integer
myColor(0) = ColorTranslator.FromHtml("#31659C")
myColor(1) = ColorTranslator.FromHtml("#CE9ACE")
myColor(2) = ColorTranslator.FromHtml("#FF65FF")
myColor(3) = ColorTranslator.FromHtml("#CE9A63")
myColor(4) = ColorTranslator.FromHtml("#CE6563")
'gwidth为图片宽度,根据字符长度自动更改图片宽度
img = New Bitmap(gwidth, 18)
g = Graphics.FromImage(img)
g.FillRectangle(WhiteBrush, 0, 0, gwidth, 18)
For I = 1 To Int(Len(CheckCode))
g.DrawString(mid(CheckCode, I, 1), (GetFont), (New SolidBrush(myColor(I - 1))), (I - 1) * 12 + 1, 1) '在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
Next
ms = New MemoryStream
img.Save(ms, ImageFormat.Png)
Response.ClearContent() '需要输出图象信息 要修改HTTP头
Response.ContentType = "image/Png"
Response.BinaryWrite(ms.ToArray())
g.Dispose()
img.Dispose()
Response.End()
End Sub
'--------------------------------------------
'函数名称:RndNum
'函数参数:VcodeNum--设定返回随机字符串的位数
'函数功能:产生数字和字符混合的随机字符串
Function RndNum(ByVal VcodeNum As Integer)
Dim Vchar As String = "0,1,2,3,4,5,6,7,8,9"
Dim VcArray() As String = split(Vchar, ",") '将字符串生成数组
Dim CheckCode As String = ""
Dim i As Byte
For i = 1 To VcodeNum
Randomize()
CheckCode = CheckCode & VcArray(Int(9 * Rnd)) '数组一般从0开始读取,所以这里为29*Rnd
Next
Return CheckCode
End Function
Private Function GetFont() As Font
Dim font1 As Font
Try
Dim collection1 As New PrivateFontCollection
collection1.AddFontFile(MyBase.Server.MapPath("font.TTF"))
font1 = New Font(collection1.Families(0), 14, fontstyle.Regular)
Catch exception1 As Exception
Throw exception1
End Try
Return font1
End Function
End Class
[/code]
[uploadfile]4,font.rar,79.1kb[/uploadfile]
|
|