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

[原创] vbs bitmap类

本帖最后由 jyswjjgdwtdtj 于 2024-4-21 15:13 编辑

相当于把wia套了个壳子 呵呵
  1. Class Bitmap
  2.     'width,height可以理解为像素矩阵的上界 与vbs数组保持一致 而非有多少像素
  3.     Private imagefile
  4.     Private argbdata
  5.     Private fso
  6.     Private picturefrom
  7.     Private formatid
  8.     Private picwidth,picheight
  9.     Private pictureto
  10.     Private ip
  11.    
  12.     Public default Property Get pixel_(x,y)
  13.         pixel_ = Me.pixel(x,y)
  14.     End Property
  15.    
  16.     Private Sub Class_Initialize
  17.         Set imagefile = CreateObject("WIA.ImageFile")
  18.         Set argbdata = CreateObject("WIA.Vector")
  19.         Set ip = CreateObject("WIA.ImageProcess")
  20.         Set fso = CreateObject("scripting.filesystemobject")
  21.     End Sub
  22.    
  23.     Public Sub LoadPicture(ByVal img_address)
  24.         picturefrom = img_address
  25.         imagefile.loadfile picturefrom
  26.         picwidth = imagefile.width - 1
  27.         picheight = imagefile.height - 1
  28.         Set argbdata = imagefile.argbdata 'as wiaVector
  29.         formatid = file.formatid
  30.     End Sub
  31.    
  32.     Private Sub filedelete(ByVal address)
  33.         If fso.fileexists(address) Then
  34.             fso.deletefile(address)
  35.         End If
  36.     End Sub
  37.    
  38.     Public Sub saveto(fileaddress)
  39.         pictureto = fileaddress
  40.         Set im = argbdata.imagefile(picwidth + 1,picheight + 1)
  41.         filedelete pictureto
  42.         im.savefile pictureto
  43.     End Sub
  44.    
  45.     Public Sub save
  46.         Call saveto(picturefrom)
  47.     End Sub
  48.    
  49.     Public Property Get pixel(x,y)
  50.         pixel = argbdata.item(y * (picwidth + 1) + x + 1)
  51.     End Property
  52.    
  53.     Public Property Let pixel(x,y,ByVal num)
  54.         argbdata.item(y * (picwidth + 1) + x + 1) = CLng(num)
  55.     End Property
  56.    
  57.     Public Property Get height()
  58.         height = picheight
  59.     End Property
  60.    
  61.     Public Property Get width()
  62.         width = picwidth
  63.     End Property
  64.    
  65.     Public Sub reset(x,y)
  66.         formatid = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"
  67.         argbdata.clear
  68.         picwidth = x
  69.         picheight = y
  70.         For i = 1 To (x + 1) * (y + 1)
  71.             argbdata.add(CLng(&hffffffff))
  72.         Next
  73.     End Sub
  74.     Public Function RGB(r,g,b)
  75.         RGB = r * &h10000 + g * &h100 + b
  76.     End Function
  77.    
  78.     Public Function tomatrix()
  79.         Dim matrix()
  80.         ReDim matrix(picwidth,picheight)
  81.         For i = 0 To picheight
  82.             For j = 0 To picwidth
  83.                 matrix(j,i) = argbdata.item(i * (picwidth + 1) + j + 1)
  84.             Next
  85.         Next
  86.         tomatrix = matrix
  87.     End Function
  88.    
  89.     Public Sub setasmatrix(matrix)
  90.         picwidth = UBound(matrix,1)
  91.         picheight = UBound(matrix,2)
  92.         Me.reset picwidth,picheight
  93.         For i = 0 To picheight
  94.             For j = 0 To picwidth
  95.                 argbdata.item(i * (picwidth + 1) + j + 1) = matrix(j,i)
  96.             Next
  97.         Next
  98.     End Sub
  99.    
  100.     Public Sub compress(value)
  101.         Set image = argbdata.imagefile(picwidth + 1,picheight + 1)
  102.         ip.Filters.Add ip.FilterInfos("Convert").FilterID
  103.         ip.Filters(1).Properties("FormatID").Value = formatid
  104.         ip.Filters(1).Properties("Quality").Value = value
  105.         Set image = ip.apply(image)
  106.         Set argbdata = image.argbdata
  107.         picwidth = image.width - 1
  108.         picheight = image.height - 1
  109.         ip.filters.remove(1)
  110.     End Sub
  111.    
  112.     Public Sub rotate(degree)
  113.         Set image = argbdata.imagefile(picwidth + 1,picheight + 1)
  114.         ip.Filters.Add ip.FilterInfos("RotateFlip").FilterID
  115.         ip.Filters(1).Properties("RotationAngle") = degree
  116.         Set image = ip.apply(image)
  117.         Set argbdata = image.argbdata
  118.         picwidth = image.width - 1
  119.         picheight = image.height - 1
  120.         ip.filters.remove(1)
  121.     End Sub
  122.    
  123.     Public Sub crop(Left,top,Right,bottom)
  124.         Set image = argbdata.imagefile(picwidth + 1,picheight + 1)
  125.         ip.Filters.Add ip.FilterInfos("Crop").FilterID
  126.         ip.Filters(1).Properties("Left") = Left
  127.         ip.Filters(1).Properties("Top") = top
  128.         ip.Filters(1).Properties("Right") = Right
  129.         ip.Filters(1).Properties("Bottom") = bottom
  130.         Set image = ip.apply(image)
  131.         Set argbdata = image.argbdata
  132.         picwidth = image.width - 1
  133.         picheight = image.height - 1
  134.         ip.filters.remove(1)
  135.     End Sub
  136.    
  137.     Public Sub scale(width,height)
  138.         Set image = argbdata.imagefile(picwidth + 1,picheight + 1)
  139.         ip.Filters.Add ip.FilterInfos("Scale").FilterID
  140.         ip.Filters(1).Properties("MaximumWidth") = width
  141.         ip.Filters(1).Properties("MaximumHeight") = height
  142.         Set image = ip.apply(image)
  143.         Set argbdata = image.argbdata
  144.         picwidth = image.width - 1
  145.         picheight = image.height - 1
  146.         ip.filters.remove(1)
  147.     End Sub
  148.    
  149. End Class
  150. Function rand(n)
  151.     Randomize
  152.     rand = Int(n * Rnd)
  153. End Function
  154. Set pic = New bitmap
  155. pic.reset 1000,1000
  156. For i = 0 To 1000
  157.     For j = 0 To 1000
  158.         pic.pixel(j,i) = pic.RGB(rand(256),rand(256),rand(256))
  159.     Next
  160. Next
  161. pic.compress 5
  162. pic.saveto "2.jpg"
复制代码
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

返回列表