Recommended: Comparison of several ways to generate static HTML files for ASP There are many benefits to converting dynamic pages to generate static Html files, such as generating html web pages is conducive to being included by search engines (especially for pages that accept dynamic parameters). When accessing the front desk, it is separated from data access, alleviating the pressure on database access and speeding up the opening of web pages.
Use ASP to generate image color verification code
49 lines of code, three files
Asp file: Code.Asp
Data files: body.Fix, Head.Fix
Usage: <img src=code.asp>
<%Option ExplicitResponse.buffer=trueNumCodeFunction NumCode() Response.Expires = -1 Response.AddHeader Pragma,no-cache Response.AddHeader cache-ctrol,no-cache dim zNum,i,j dim Ados,Ados1 Randomize timer zNum = cint(8999*Rnd 1000) Session(GetCode) = zNum dim zimg(4),NStr NStr=cstr(zNum) For i=0 to 3 zimg(i)=cint(mid(NStr,i 1,1)) Next dim Pos set Ados=Server.CreateObject(Adodb.Stream) Ados.Mode=3 Ados.Type=1 Ados.Open set Ados1=Server.CreateObject(Adodb.Stream) Ados1.Mode=3 Ados1.Type=1 Ados1.Open Ados.LoadFromFile(Server.mappath(body.Fix)) Ados1.write Ados.read(1280) for i=0 to 3 Ados.Position=(9-zimg(i))*320 Ados1.Position=i*320 Ados1.write ados.read(320) next Ados.LoadFromFile(Server.mappath(head.fix)) Pos=lenb(Ados.read()) Ados.Position=Pos for i=0 to 9 step 1 for j=0 to 3 Ados1.Position=i*32 j*320 Ados.Position=Pos 30*ji*120 Ados.write ados1.read(30) next next Response.ContentType = image/BMP Ados.Position=0 Response.BinaryWrite Ados.read() Ados.Close:set Ados=nothing Ados1.Close:set Ados1=nothingEnd Function' Asp code Created by BlueIdea.COM Web Team V37 2003-7-25%> |
Code explanation:
| Response.buffer=true NumCode Function NumCode() Response.Expires = -1 Response.AddHeader Pragma,no-cache Response.AddHeader cache-ctrol,no-cache dim zNum,i,j dim Ados,Ados1 Randomize timer |
Generate random four-digit numbers:
| zNum = cint(8999*Rnd 1000) |
Pass to session
| Session(GetCode) = zNum |
This for loop is to put random numbers into an array with subscript 3, which is convenient for providing the subsequent array transformation
| dim zimg(3),NStr NStr=cstr(zNum) For i=0 to 3 zimg(i)=cint(mid(NStr,i 1,1)) Next dim Pos |
'Define two ADODB.Stream binary objects for image data operation:
| set Ados=Server.CreateObject(Adodb.Stream) Ados.Mode=3 Ados.Type=1 Ados.Open set Ados1=Server.CreateObject(Adodb.Stream) Ados1.Mode=3 Ados1.Type=1 Ados1.Open |
'Load 0~9 digital data 10x100, Gbr array data, each 320 byte, 10 digits 3200 byte
'BGR one point, 10x10 points one number, one point three bytes (8 bits in binary, 00~FF in hexadecimal)
'A line of 10 points 30 bytes line end mark 00 00 2 bytes 32 bytes, so a 10x100 image with a width smaller than the length of each number 10x10 is 320 bytes
'If the length is larger than the width, there will be no line end mark of 0000, which is directly 300 bytes.
These are the BMP 24bit data details
'As for the head, it is also very simple, including length and width, image start marking, etc. ~~ It is only 54 bytes, far less complicated than jpg or something
| Ados.LoadFromFile(Server.mappath(body.Fix)) Ados1.write Ados.read(1280) |
'The first for loop, extract the corresponding four numbers from the 10X100 digital array in the order of random numbers generated
But vertical array of numbers
| for i=0 to 3 Ados.Position=(9-zimg(i))*320 Ados1.Position=i*320 Ados1.write ados.read(320) next |
'Clear the data of the used ADOS and call in the header file that replaces the new image header 54 bytes
| Ados.LoadFromFile(Server.mappath(head.fix)) Pos=lenb(Ados.read()) Ados.Position=Pos |
'Specify the Pos position, and then offset the position by 54 bytes to add graphic data
The second for loop performs array transformation of numbers, converting vertical blocks into horizontal blocks
The method is to extract 4 times of 320 bytes and write to the ados object, and then extract the image data that is offset from the second line.
30 bytes is because bmp width is greater than long without 00 00 line end mark
| The following is the quoted content: for i=0 to 9 step 1 for j=0 to 3 Ados1.Position=i*32 j*320 Ados.Position=Pos 30*ji*120 Ados.write ados1.read(30) next next Ados.Position=0 response.BinaryWrite sends image data directly to the client Response.ContentType = image/BMP Response.BinaryWrite Ados.read() Ados.Close:set Ados=nothing Ados1.Close:set Ados1=nothing End Function |
Share: About the separation of Asp code and page In order to avoid maintenance difficulties caused by the misunderstanding of ASP program TML code, this article introduces a method to use templates to separate programs and pages to make programming easier. When using ASP to create a site, an ASP file often appears, program code and HTM