建一個YanzhenCard.aspx文件
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),尼金平企業(yè)網(wǎng)站建設(shè),尼金平品牌網(wǎng)站建設(shè),網(wǎng)站定制,尼金平網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,尼金平網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
保留%@ Page Language="VB" AutoEventWireup="false" CodeFile="YanzhenCard.aspx.vb" Inherits="YanzhenCard" %,其余刪除
如下是代碼:
Imports System.IO
Imports System.Drawing
Partial Class YanzhenCard
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim VNum As String = GenerateRandom(4)
Session("VNum") = VNum
ValidateCode(VNum)
End If
End Sub
Private Sub ValidateCode(ByVal VNum As String)
Dim Gheight As Integer = CType((VNum.Length * 15), Integer)
Dim Img As Bitmap = New Bitmap(Gheight, 20) 'Bitmap是用于處理由像素數(shù)據(jù)定義的圖像
Dim g As Graphics = Graphics.FromImage(Img) 'Graphics.FromImage 從指定的Image創(chuàng)建新的Graphics
g.DrawString(VNum, New Font("Arial", 12), New SolidBrush(Color.Red), 5, 0) 'g.DrawString()在指定位置并且用指定的Brush和Font對象繪制指定的文本字符串。
Dim ms As MemoryStream = New MemoryStream '創(chuàng)建其支持存儲區(qū)為內(nèi)存的流。
Img.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
Response.ClearContent()
Response.ContentType = "image/PNG"
Response.BinaryWrite(ms.ToArray)
Response.Write(VNum)
g.Dispose()
Img.Dispose()
Response.End()
End Sub
Private Shared constant As Char() = {"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"}
Public Shared Function GenerateRandom(ByVal Length As Integer) As String
Dim newRandom As System.Text.StringBuilder = New System.Text.StringBuilder(26)
Dim rd As Random = New Random
Dim i As Integer = 0
While i Length
newRandom.Append(constant(rd.Next(26)))
System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
End While
Return newRandom.ToString
End Function
End Class
調(diào)用方法:asp:ImageButton ID="Yanzhengma_Card" runat="server" ImageUrl="YanzhenCard.aspx" Height="20px" Width="75px" BorderColor="#6699cc" BorderStyle="Solid" BorderWidth="1px" /
實現(xiàn)過程,在需要調(diào)用的頁面使用image控件調(diào)用驗證碼生成的頁面,驗證碼生成的頁面生成驗證圖片且將驗證信息保存至Session中,調(diào)用頁面中通過Session判斷用戶是否輸入正確。道理是這樣,以下看代碼:
生成驗證碼頁面:
gif.ASPx
%@importnamespace="System"%
%@importnamespace="System.io"%
%@importnamespace="System.Drawing"%
%@importnamespace="System.Drawing.Imaging"%
scriptlanguage="VB"runat="server"
SubPage_Load(ByValSenderAsObject,ByValeAsEventArgs)
'RndNum為生成隨機碼的函數(shù),
DimVNumAsString=RndNum(4)'該值為生成驗證碼的位數(shù)
Session("VNum")=VNum'讀取Session
ValidateCode(VNum) '根據(jù)Session生成圖片
EndSub
'--------------------------------------------
'生成圖象驗證碼函數(shù)
SubValidateCode(ByValVNum)
DimImgAsSystem.Drawing.Bitmap
DimgAsGraphics
DimmsAsMemoryStream
DimgheightAsInteger=Int(Len(VNum)*14)
'gheight為圖片寬度,根據(jù)字符長度自動更改圖片寬度
Img=NewBitmap(gheight,24)
g=Graphics.FromImage(Img)
g.DrawString(VNum,(NewFont("Arial",12)),(NewSolidBrush(Color.Red)),3,3)'在矩形內(nèi)繪制字串(字串,字體,畫筆顏色,左上x.左上y)
ms=NewMemoryStream()
Img.Save(ms,ImageFormat.Png)
Response.ClearContent()'需要輸出圖象信息要修改HTTP頭
Response.ContentType="image/Png"
Response.BinaryWrite(ms.ToArray())
g.Dispose()
Img.Dispose()
Response.End()
EndSub
'--------------------------------------------
'函數(shù)名稱:RndNum
'函數(shù)參數(shù):VCodeNum--設(shè)定返回隨機字符串的位數(shù)
'函數(shù)功能:產(chǎn)生數(shù)字和字符混合的隨機字符串
FunctionRndNum(ByValVcodeNum)
DimVcharAsString="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F(xiàn),G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z"'需要使用中文驗證,可以修改這里和ValidateCode函數(shù)中的字體
DimVcArray()AsString=Split(Vchar,",")'將字符串生成數(shù)組
DimVNumAsString=""
DimiAsByte
Fori=1ToVcodeNum
Randomize()
VNum=VNumVcArray(Int(35*Rnd()))'數(shù)組一般從0開始讀取,所以這里為35*Rnd
Next
ReturnVNum
EndFunction
/script
調(diào)用頁面:
demo.ASPx
scriptlanguage="VB"Runat="Server"
SubPage_Load(Senderasobject,easeventargs)
dimVNumasstring=Session("VNum")
Session.Abandon()
ViewState("VNum")=VNum
EndSub
'下面的事件代碼是用來測試驗證碼
SubbtnSubmit_click(senderasobject,easeventargs)
'判斷輸入的驗證碼與所給是否相同
IftxtValidateCode.Text=CStr(ViewState("VNum"))Then
ensonShow.text="fontcolor='red'提示:驗證通過/font"
Else
ensonShow.text="所填寫的驗證碼與所給的不符"
EndIf
EndSub
/script
html
body
formrunat="server"
divalign="center"
tablewidth="750"
tr
tdvalign="middle"驗證碼:/td
!--調(diào)用驗證圖片生成頁---
tdvalign="top"asp:Imageid="Image1"runat="server"ImageUrl="gif.aspx"http://td
/tr
tr
tdvalign="top"輸入驗證碼:/td
tdvalign="top"asp:textboxID="txtValidateCode"runat="server"TextMode="SingleLine"/fontcolor="#FF0000"size="2"*區(qū)分大小寫/font/td
/tr
tr
tdcolspan="2"valign="top"align="center"asp:buttonID="btnSubmit"runat="server"Text="驗證"onclick="btnSubmit_click"/
asp:labelID="ensonShow"runat="server"/
/td
/tr
/table
/div
/form
/body
/html
這個是可以使用api來做的。圖像處理之類的函數(shù)。
另外,還有一種方法是將所有的字符和數(shù)字做成不同的小圖片進行拼接而成的一個字符串序列;
小圖片要自己去做;效果就可以任意展現(xiàn)了。
Dim CtrlRange As Object
On Error Resume Next
Do
Err.Clear()
CtrlRange = Ie.Document.Body.DomElement.createControlRange()
CtrlRange.Add(Ie.Document.DomDocument.GetElementById("verifypic"))
CtrlRange.execCommand("Copy") '獲取驗證碼
If Clipboard.ContainsData(DataFormats.Bitmap) Then
PictureBox1.Image = Clipboard.GetData(DataFormats.Bitmap) '在Picture1控件上顯示
End If
If Err.Number 0 Then WaitingMS(100) ‘延時
Loop While Err.Number 0
以前找的 vbnet里也可以用
Imports?System
Imports?System.Data
Imports?System.Configuration
Imports?System.Collections
Imports?System.Web
Imports?System.Web.Security
Imports?System.Web.UI
Imports?System.Web.UI.WebControls
Imports?System.Web.UI.WebControls.WebParts
Imports?System.Web.UI.HtmlControls
Imports?System.Drawing
Partial?Public?Class?VerifyCode
Inherits?System.Web.UI.Page
Protected?Sub?Page_Load(ByVal?sender?As?Object,?ByVal?e?As?System.EventArgs)
Session("VerifyCode")?=?MakeValidateCode()
CreateImage(Session("VerifyCode").ToString())
End?Sub
Private?Sub?CreateImage(ByVal?checkCode?As?String)
Dim?iwidth?As?Integer?=?CInt(checkCode.Length?*?12)?+?10
Dim?image?As?New?System.Drawing.Bitmap(iwidth,?25)
Dim?g?As?Graphics?=?Graphics.FromImage(image)
g.Clear(Color.White)
'定義顏色
Dim?c()?As?Color?=?{?Color.Black,?Color.Red,?Color.DarkBlue,?Color.Green,?Color.Orange,?Color.Brown,?Color.DarkCyan,?Color.Purple?}
'定義字體
Dim?font()?As?String?=?{?"Verdana",?"Microsoft?Sans?Serif",?"Comic?Sans?MS",?"Arial",?"宋體"?}
Dim?rand?As?New?Random()
'隨機輸出噪點
For?i?As?Integer?=?0?To?4
Dim?x?As?Integer?=?rand.Next(image.Width)
Dim?y?As?Integer?=?rand.Next(image.Height)
g.DrawRectangle(New?Pen(Color.LightGray,?0),?x,?y,?1,?1)
Next?i
'輸出不同字體和顏色的驗證碼字符
For?i?As?Integer?=?0?To?checkCode.Length?-?1
Dim?cindex?As?Integer?=?rand.Next(7)
Dim?findex?As?Integer?=?rand.Next(5)
Dim?f?As?Font?=?New?System.Drawing.Font("宋體",?12,?System.Drawing.FontStyle.Regular)
Dim?b?As?Brush?=?New?System.Drawing.SolidBrush(c(cindex))
Dim?ii?As?Integer?=?4
If?(i?+?1)?Mod?2?=?0?Then
ii?=?2
End?If
g.DrawString(checkCode.Substring(i,?1),?f,?b,?3?+?(i?*?12),?ii)
Next?i
'畫一個邊框
g.DrawRectangle(New?Pen(Color.Black,?0),?0,?0,?image.Width?-?1,?image.Height?-?1)
'輸出到瀏覽器
Dim?ms?As?New?System.IO.MemoryStream()
image.Save(ms,?System.Drawing.Imaging.ImageFormat.Jpeg)
Response.ClearContent()
Response.ContentType?=?"image/Jpeg"
Response.BinaryWrite(ms.ToArray())
g.Dispose()
image.Dispose()
End?Sub
Private?Function?MakeValidateCode()?As?String
'0?i?o
Dim?s()?As?Char?=?{?"0"c,?"1"c,?"2"c,?"3"c,?"4"c,?"5"c,?"6"c,?"7"c,?"8"c,?"9"c,?"A"c,?"B"c,?"C"c,?"D"c,?"E"c,?"F"c,?"G"c,?"J"c,?"K"c,?"L"c,?"M"c,?"N"c,?"P"c,?"Q"c,?"R"c,?"S"c,?"T"c,?"U"c,?"V"c,?"W"c,?"X"c,?"Y"c,?"Z"c?}
Dim?num?As?String?=?""
Dim?r?As?New?Random()
For?i?As?Integer?=?0?To?3
num?=?s(r.Next(0,?s.Length?-?1)).ToString()
Next?i
Return?num
End?Function
End?Class