vba 读取图像的像素颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16528319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read pixel colors of an image
提问by KM Hs
In VBA, how can I read the color value of each pixel of in an image?
在 VBA 中,如何读取图像中每个像素的颜色值?
I found this solutionin VB 6.0 but it doesn't apply directly in VBA.
我在 VB 6.0 中找到了这个解决方案,但它不能直接应用于 VBA。
回答by Wild138
Try the solution posted on this site here : http://sim0n.wordpress.com/2009/03/27/vba-q-how-to-get-pixel-colour/
尝试在此网站上发布的解决方案:http: //sim0n.wordpress.com/2009/03/27/vba-q-how-to-get-pixel-colour/
I had to change a ByRef to a ByVal but apart from that it works well. Insert a picture using Insert > Picture and assign a macro to the on click event . I've just made it set the colour of cell A1 to the colour you click on, but I'm sure you get the idea.
我不得不将 ByRef 更改为 ByVal,但除此之外,它运行良好。使用“插入”>“图片”插入图片并将宏分配给点击事件。我刚刚将单元格 A1 的颜色设置为您单击的颜色,但我相信您明白了。
#If VBA7 Then
Private Declare PtrSafe Function GetPixel Lib "gdi32" (ByVal hdc As LongPtr, ByVal x As Long, ByVal y As Long) As Long
Private Declare PtrSafe Function GetCursorPos Lib "user32" (ByRef lpPoint As POINT) As LongPtr
Private Declare PtrSafe Function GetWindowDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
#Else
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINT) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
#End If
Private Type POINT
x As Long
y As Long
End Type
Sub Picture1_Click()
Dim pLocation As POINT
Dim lColour As Long
Dim lDC As Variant
lDC = GetWindowDC(0)
Call GetCursorPos(pLocation)
lColour = GetPixel(lDC, pLocation.x, pLocation.y)
Range("a1").Interior.Color = lColour
End Sub
To use it, place a picture in a worksheet, right click on the image and assign this macro to it.
要使用它,请将图片放在工作表中,右键单击该图像并将此宏分配给它。