读取图像的像素颜色

在VBA中,如何读取图像中每个像素的颜色值?

我在VB 6.0中find了这个解决scheme ,但是它不直接应用于VBA。

试试这个网站上发布的解决scheme: http : //sim0n.wordpress.com/2009/03/27/vba-q-how-to-get-pixel-colour/

我不得不将ByRef更改为ByVal,但除此之外它运作良好。 使用“插入”>“图片”插入图片,然后将macros指派给点击事件。 我只是把单元格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 

要使用它,在工作表中放置一个图片,右键单击图像,并将其分配给它。