[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创] vbs bitmap类

本帖最后由 jyswjjgdwtdtj 于 2023-2-27 20:59 编辑

改了一版,删去了原来的line,shape函数
添加了crop的负数形式,即向外拓张abs(负数)个白色像素
添加读写pixel属性
读:
返回一个BitmapColor对象,直接使用返回argb值,有red,green,blue,alpha,argb四个属性
写:
写入一个argb值,修改图片
  1. class Bitmap
  2. private imagefile
  3. private argbdata
  4. private imagevector
  5. private fso
  6. private from
  7. private imagetype
  8. public pixels()
  9. private mywidth,myheight
  10. public default function cons(byval address)
  11. loadpicture address
  12. set cons=me
  13. end function
  14. private sub class_initialize
  15. set imagefile= createobject("WIA.ImageFile")
  16. set imagevector= createobject("WIA.Vector")
  17. set fso= createobject("scripting.filesystemobject")
  18. end sub
  19. private sub loadpicture(byval img_address)
  20. from=img_address
  21. imagefile.loadfile from
  22. mywidth=imagefile.width:myheight=imagefile.height
  23. redim pixels(mywidth,myheight)
  24. set argbdata=imagefile.argbdata
  25. dim k:k=0
  26. dim i,j
  27. for i=1 to myheight
  28. for j=1 to mywidth
  29. k=k+1
  30. pixels(j,i)=argbdata(k)
  31. next
  32. next
  33. end sub
  34. public sub turn(byval degree)
  35. dim newpixels
  36. newpixels=pixels
  37. dim i,j,t
  38. select case degree
  39. case 90
  40. redim pixels(myheight,mywidth)
  41. for i=1 to mywidth
  42. for j=1 to myheight
  43. pixels(j,i)=newpixels(i,myheight+1-j)
  44. next
  45. next
  46. t=mywidthmywidth=myheightmyheight=t
  47. case 270
  48. redim pixels(myheight,mywidth)
  49. for i=1 to mywidth
  50. for j=1 to myheight
  51. pixels(j,i)=newpixels(mywidth+1-i,j)
  52. next
  53. next
  54. t=mywidthmywidth=myheightmyheight=t
  55. case 180
  56. redim pixels(mywidth,myheight)
  57. for i=1 to mywidth
  58. for j=1 to myheight
  59. pixels(i,j)=newpixels(mywidth+1-i,myheight+1-j)
  60. next
  61. next
  62. end select
  63. end sub
  64. private function dec(byval sixteen)
  65. dim result
  66. result=0
  67. sixteen=ucase(sixteen)
  68. dim i,l
  69. l=len(sixteen)
  70. for i=l to 1 step -1
  71. dim ir
  72. ir=mid(sixteen,i,1)
  73. if isnumeric(ir) then
  74. result=result+16^(l-i)*ir
  75. else
  76. result=result+16^(l-i)*(asc(ir)-65+10)
  77. end if
  78. next
  79. dec=result
  80. end function
  81. private sub filedelete(byval address)
  82. if fso.fileexists(address) then fso.deletefile(address)
  83. end sub
  84. public sub crop(top,bottom,left,right)'这里用了“下标越界”错误来判断,或许可能也许大概有些问题,有兴趣改成正紧的
  85. on error resume next
  86. top=int(top):bottom=int(bottom):left=int(left):right=int(right)
  87. dim newpixels
  88. newpixels=pixels
  89. if mywidth-left-right<0 or myheight-top-bottom<0 then redim pixels(1,1):myheight=1:mywidth=1:exit sub
  90. redim pixels(mywidth-left-right,myheight-top-bottom)
  91. dim ip,jp,i,j
  92. ip=0
  93. for i=top+1 to myheight-bottom
  94. ip=ip+1:jp=0
  95. for j=left+1 to mywidth-right
  96. jp=jp+1
  97. pixels(jp,ip)=newpixels(j,i)
  98. if err.number=9 or j=0 or i=0 then pixels(jp,ip)=&HFFFFFF:err.clear
  99. next
  100. next
  101. mywidth=mywidth-left-right:myheight=myheight-top-bottom
  102. end sub
  103. private function setcolor(byval argb)
  104. argb=hex(argb)
  105. argb=string("0",8-len(argb))&argb
  106. alpha=dec(mid(argb,1,2))
  107. red=dec(mid(argb,3,2))
  108. green=dec(mid(argb,5,2))
  109. blue=dec(mid(argb,7,2))
  110. argb=dec(argb)
  111. setcolor= "class BitmapColor" &vbcrlf&_
  112. "public property get red()" &vbcrlf&_
  113. "red="&red &vbcrlf&_
  114. "end property" &vbcrlf&_
  115. "public property get green()" &vbcrlf&_
  116. "green="&green &vbcrlf&_
  117. "end property" &vbcrlf&_
  118. "public property get blue()" &vbcrlf&_
  119. "blue="&blue &vbcrlf&_
  120. "end property" &vbcrlf&_
  121. "public property get alpha()" &vbcrlf&_
  122. "alpha="&alpha &vbcrlf&_
  123. "end property" &vbcrlf&_
  124. "public property get argb()" &vbcrlf&_
  125. "argb="&argb &vbcrlf&_
  126. "end property" &vbcrlf&_
  127. "public default function dargb()"&vbcrlf&_
  128. "dargb="&argb &vbcrlf&_
  129. "end function" &vbcrlf&_
  130. "end class"
  131. end function
  132. public sub save
  133. dim i,j
  134. dim k:k=0
  135. imagevector.clear
  136. for i=1 to myheight
  137. for j=1 to mywidth
  138. imagevector.add pixels(j,i)
  139. k=k+1
  140. next
  141. next
  142. set im=imagevector.imagefile(mywidth,myheight)
  143. filedelete from
  144. im.savefile from
  145. imagevector.clear
  146. end sub
  147. public property get pixel(x,y)
  148. execute(setcolor(pixels(x,y)))
  149. set pixel=new BitmapColor
  150. end property
  151. public property let pixel(x,y,byval num)
  152. pixels(x,y)=hex(int(num))
  153. end property
  154. public property get height()
  155. height=myheight
  156. end property
  157. public property get width()
  158. width=mywidth
  159. end property
  160. public sub reset()
  161. for i=1 to mywidth
  162. for j=1 to myheight
  163. pixels(i,j)=&HFFFFFF
  164. next
  165. next
  166. end sub
  167. end class
  168. set pic=(new bitmap)("1.bmp")
  169. pic.crop -30,-40,-20,-60
  170. pic.reset
  171. for i=50 to 150
  172. msgbox pic.pixel(i,i)
  173. wscript.echo pic.pixel(i,i).red
  174. pic.pixel(i,i)=0
  175. next
  176. pic.save
复制代码
至于像素的其他方法,需要自己实现(自由度高了?)
1

评分人数

vbs居然有constructor,屑微软的文档里边怎么没写

TOP

本帖最后由 jyswjjgdwtdtj 于 2023-2-20 19:01 编辑

回复 2# 老刘1号


    呃呃呃 你别误会 他可以不叫constructor 也可以叫wobushigouzaohanshu
这里利用了一个vbs独有的default
比如:
  1. class a
  2. public default function c(var)'叫啥名都可以
  3. msgbox var
  4. set c=me
  5. end function
  6. end class
  7. set b=new a
  8. b(1)
  9. 所以你也可以这样
  10. set d=(new a)(111)'可以利用类名来直接调用这个默认函数(子程序)
复制代码
这里是demon大佬想出来的一个歪招
就是这个函数返回“ME”,就类似于js里的this
来达到构造函数的效果
1

评分人数

TOP

返回列表