请问怎样往 ADODB.Stream 写入二进制内容?

slawdan 2006-11-30 01:13:54
我使用ADODB.stream 读取图片,并拼合了一个图片验证码,字节流暂存在一个ADODB.Stream中
现在想往 图片中添加随机干扰点,使用ADODB.Stream.write CHRB(0) 添加不进去,3001错误:参数类型不正确,或者超出范围

请问应该怎么改,或者有其他的替代方案也可以


还没搞清楚VBS中的变量类型的朋友请不要随意回答,虚心学习也是一种美德,谢谢


'------------------------

'部分代码如下:

'-----------------------------------
function AddNoise(oStream,intDotCount)

'oStream 中存放的是一个4800字节的无BMP文件头的BMP图片,高20 像素,宽80像素
'debug 是 自定义的调试函数,用来输出调试信息到log文件里

if not IsObject(oStream) then exit function

dim intPosOrigin,intPos,intLength
dim i,j,intX,intY,intColor,bytColor
dim oStream2
set oStream2 = server.CreateObject("ADODB.Stream")
oStream2.Type = 1
oStream2.mode = 3
oStream2.open

intPosOrigin = oStream.position '记录ostream的原始游标位置
debug "当前位置",intPosOrigin

oStream.position = 0
intLength = int(oStream.size/3) '因为是BMP的图片,3个字节表示一个像素点

debug "流长度(像素/字节)",intLength & "/" & oStream.size

randomize
on error resume next

bytColor = oStream.read(3) '直接从oStream中读一个像素。此时typename(bytColor) = "byte()"
' bytColor = array(cbyte(0),cbyte(0) , cbyte(0)) ' 这样生成,typename(bytColor ) = "variable"

oStream.position = 0 ' 将oStream游标重置

j = Rnd() '生成随机数
if err then
debug "添加噪点2错误a",err.Description
err.Clear
end if
if j > 0.5 then ' 50%的可能性保持原来内容 oStream2.Write oStream.read(3) '
if err then
debug "添加噪点2错误b1-" & err.Number,err.Description
err.Clear
end if
else '写入干扰点
oStream2.write bytColor '------这里出错。不出错的时候显示一个只有最下一条线有内容而其他地方都是黑色的图片
if err then
debug "添加噪点2错误b21-" & err.Number,err.Description
err.Clear
end if
end if
if err then
debug "添加噪点2错误e",err.Description
err.Clear
end if

debug "ostream.size",oStream.size
debug "ostream2.size",oStream2.size
debug "ostream.position",ostream.position
debug "ostream2.position",ostream2.position

'读取剩余像素
oStream2.write oStream.read(oStream.size - intLength / 100 * 3)

debug "oStream2.size",oStream2.Size

'把临时对象中内容复制回去
oStream2.position = 0
ostream2.copyto oStream,intLength * 3

set AddNoise = oStream

if err then
debug "添加噪点2错误",err.Description
err.Clear
end if
end function
...全文
925 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
smartcatiboy 2007-03-21
  • 打赏
  • 举报
回复
可以写入

比方stm为现有二进制图片留,搂主可这样处理

stm.mode=3
stm.type=1
stm.open
stm.read rst("img")
iHeadSize=52 '//bmp文件头长度,你还要查查,记不清

iLen=stm.size
if iLen<iHeadSize then 错误图片
iImgSize=iLen-iSize

'//添加100个杂点,具体视文件色彩数目,单色1个字节会造成4个像素变化,全色则是颜色变化

for i=1 to 100
j=cint(rnd*(iImgSize-1))+iHeadSize+1
k=cint(rnd*(iImgSize-1))+iHeadSize+1
stm.position=j
b=stm.read(1)
stm.position=k
stm.write b
next
zhangxiqi 2007-03-20
  • 打赏
  • 举报
回复
up
cloudgamer 2006-12-19
  • 打赏
  • 举报
回复
高手
tangqiaojie 2006-12-18
  • 打赏
  • 举报
回复
学习,mark
btbtd 2006-12-11
  • 打赏
  • 举报
回复
哈哈, 你这人这阵子跑那了, N 久没见呀, 不过ASP我也很久没来了.
这个我本来是有点体会滴, 不过现在时间不让我说呀, ^^闪...
xilo 2006-12-09
  • 打赏
  • 举报
回复
顶一下,觉得不错....
smartcatiboy 2006-12-09
  • 打赏
  • 举报
回复
这个问题我前几天也在搞,弄得头昏脑葬。

asp中的所谓“byte”是var.byte,stream读取的是byte类型,asp无法直接操作。

有一种变通的办法就是用midb函数读取,但是也仅限于读取,甚至要比较也很困难,更不要说赋值。

我在网上找到唯一的办法是借用组件,或者自己编dll

期待高手来指点。
ATHENA112 2006-12-04
  • 打赏
  • 举报
回复
腾讯的那种验证码不知道有没有人能做出来
muxrwc 2006-12-04
  • 打赏
  • 举报
回复
up.
pzhuyy 2006-12-04
  • 打赏
  • 举报
回复
难道腾讯的验证码不是人做出来的:).
dong127 2006-12-04
  • 打赏
  • 举报
回复
mark
slawdan 2006-12-01
  • 打赏
  • 举报
回复
我顶~
slawdan 2006-12-01
  • 打赏
  • 举报
回复
现在生成的验证码肉眼都已经很难认出来了,有被灌水机骚扰而需要特殊验证码的朋友可以到

http://rodin.wecd.cn/demo/getcode/testgetcode2.asp
观看示例

http://rodin.wecd.cn/demo/getcode/log2.txt
查看调试日志

源程序和更为复杂的功能稍后提供下载
slawdan 2006-12-01
  • 打赏
  • 举报
回复
嗯~~
谢谢
已经用替代的方法在做了

先ADODB.Stream.type = 1
把二进制读取到第一个数组中

然后ADODB.Stream.Type = 2
把文本读取到第二个数组中~

两个数组进行对照操作~~~这样有个问题是会丢失大于 7F 的字节

还好我只是用这个东西做模板,只需要 00 和 非00 两种字节即可~

谢谢楼上
wtogether 2006-12-01
  • 打赏
  • 举报
回复
ADODB.Stream的Write方法只支持Byte(),Read()方法读取的也是Byte(),Byte()的VarType类型为8209,即vbByte(17) + vbArray(8192)

Byte()类型的数据在ASP里面能用MidB/InStrB/LeftB这些二进制函数转换成String,但是不能做为数组操作,即使是ChrB,也是String,而不是Byte,so,ADODB.Stream.Write ChrB(0)的参数是错误的
vb可以实现,因为有数据类型限制

Dim tmp(1) as Byte
tmp(0) = 13
tmp(1) = 10
Dim objFile As ADODB.Stream
Set objFile = New ADODB.Stream
objFile.Type = adTypeBinary
objFile.Mode = adModeReadWrite
objFile.open
objFile.Write tmp
objFile.Close
Set objFile = Nothing

这个方法在ASP里面实现不了,ASP声明的数组是Variant(),即vbVariant(12) + vbArray(8192) = 8204
slawdan 2006-12-01
  • 打赏
  • 举报
回复


已经改换方法,把字节数组读取到数组里,然后在数组的元素之间进行添加干扰的操作~~

但是我还是很想知道为什么

with ADODB.Stream
.Type = 1
.Mode = 3
.Write chrb(0)
end with

不行~ 3001 错误。参数不在可接受的范围之内
jspadmin 2006-12-01
  • 打赏
  • 举报
回复
你是想无组件实现图片上加写验证码吗?
slawdan 2006-11-30
  • 打赏
  • 举报
回复
即使有,也都是 从Stream里读出来又写到Stream里的~ stream1.write stream2.read 之类的~

唉~~哭了……
slawdan 2006-11-30
  • 打赏
  • 举报
回复
我搜到的代码都是直接 Response.BinaryWrite 出去的,没有直接写二进制到 Adodb.Stream 里的~
  • 打赏
  • 举报
回复
这类代码太多了,直接搜索吧....不难
加载更多回复(4)

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧