求助——vba 中如何获取image 里面的图片某点的像素颜色?
在VBA中如何获取图片某像素点的颜色值?
在vb 6.0中如下几种方法是简便可行的
方法1:
Dim str As String
Private Sub Image1_Click()
MsgBox str
End Sub
Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
str = "你选取的所在点的颜色值是" & Hex(Point(X, Y))
End Sub
方法2:
如何获取图片某点的颜色值
'需控件:Picture1、Label1、Label2
Private Sub Form_Load()
Label1.Width = Me.Width
Label1.Caption = "在图片框上移动鼠标,可查看某个点的颜色值"
Picture1.ScaleMode = vbPixels '设置图片框的单位为:像素
'假设有图片:C:\My1.jpg
'图片类型为 bmp ico rle wmf emf GIF jpg 皆可
Picture1.Picture = LoadPicture("C:\My1.jpg")
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim Se As Long, R As Long, G As Long, B As Long
Se = Picture1.Point(X, Y) '获得像素点 x,y 的颜色值
Label2.BackColor = Se '设置 Label2 的背景色为 Se
Call GetRGB(Se, R, G, B) '将颜色值 Se 分解为红、绿、蓝
Label1.Caption = "当前像素点 " & X & "," & Y & " 的颜色(红绿蓝):" & R & "," & G & "," & B
End Sub
Private Sub GetRGB(Se As Long, R As Long, G As Long, B As Long)
Dim xx As String
xx = Hex(Se)
R = Len(xx)
If R < 6 Then xx = String(6 - R, "0") & xx
B = "&H" & Left(xx, 2)
G = "&H" & Mid(xx, 3, 2)
R = "&H" & Mid(xx, 5, 2)
End Sub
然而这两个方法在vba中都不可用
方法一在vba中找不到point方法
方法二在vba中没有picturebox控件
我就是想获取指定一个图片的像素点的颜色
大家谁有办法
比如利用API
或者想办法把picturebox引入VBA
最好是最简便的方法
先谢谢了
[解决办法]
对API不熟。
你应该先到VC/MFC论坛提问,如何用API获取像素颜色,然后再转换成vba语言。
[解决办法]
方法一、在VB6中,把你得代码封装一下,在VBA里调用
方法二、直接调用API