# 數字和字母組合并產生圖片的驗證碼祥解

ycodenum=4 '驗證碼的位元，或者說成個數
for i=1 to ycodenum
Randomize '初始化隨機數發生器
ycode=ycode&Int((9*Rnd)) 'rnd是隨機數，從0到1之間的任意實數，這裡獲得0到9之間的整數
next

response.write ycode '就可以輸出數字驗證碼（4位）

ychar="0,1,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" '將數字和大寫字母組成一個字串
yc=split(char,",") '將字串產生數組
ycodenum=4
for i=1 to ycodenum
Randomize
ycode=ycode&yc(Int((35*Rnd))) '數組一般從0開始讀取，所以這裡為35*Rnd
next

response.write ycode

ychar="0,1,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" '將數字和大寫字母組成一個字串
yc=split(char,",") '將字串產生數組
ycodenum=4
for i=1 to ycodenum
Randomize
ycode=ycode&yc(Int((35*Rnd))) '數組一般從0開始讀取，所以這裡為35*Rnd
next

Response.Clear
Response.ContentType="image/gif"
set obj=Server.CreateObject("shotgraph.image")
x=55 '圖片的寬
y=26 '圖片的高
obj.CreateImage x,y,8 '8是圖片的顏色8位
obj.SetColor 0,55,126,222
obj.SetColor 1,255,255,255

obj.CreatePen "PS_SOLID",1,0
obj.SetBgColor 0
obj.Rectangle 0,0,x-1,y-1
obj.SetBkMode "TRANSPARENT"
obj.CreateFont "Arial",136,18,1,False,False,False,False
obj.SetTextColor 1
obj.TextOut 5,4,ycode&" "

img=obj.GifImage(-1,1,"")
Response.BinaryWrite (img)

OK，這樣就完成了，我們來看看效果吧！

## E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

## Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD