How do I extract Red, Green and Blue values from the value that is returned by GetPixel
Printable View
How do I extract Red, Green and Blue values from the value that is returned by GetPixel
Code:'red value
dim retval as long
retval = GetRValue(GetPixel(HDC,x,y))
'green & blue
GetBValue & GetGValue
Sorry but GetRValue() if a VC++ function not a VB function
' Test routine to substract color components
Private Sub Command1_Click()
Dim Red As Long, Green As Long, Blue As Long
Dim Color As Long
' Create a color to test the sub
Color = RGB(150, 175, 200)
' Extract color components
Red = Color And &HFF
Green = (Color \ 256) And &HFF
Blue = (Color \ 256 \ 256) And &HFF
' Show
MsgBox "Red=" & Red ' Must be 150
MsgBox "Green=" & Green ' Must be 175
MsgBox "Blue=" & Blue ' Must be 200
End Sub
Code:Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
lColor = GetPixel(Picture1.hdc, X, Y)
'Get Red, Green and Blue
lRed = lColor Mod &H100
lGreen = Int(lColor / &H100) Mod &H100
lBlue = Int(lColor / &H10000) Mod &H100
End Sub
A regular VB form has Point(x,y) methode for setting/getting a pixel, You don't need the GetPixel() function from the API.
Note: the GetPixel() API function's default mapping mode is in pixels and de default VB mapping mode is Twips.
You forgot to mention how much slower Point and PSet are compared to GetPixel and SetPixel.
Your right, SetPixel() if about 50% faster then PSet () and GetPixel is about 70% faster then Point()
SetPixelV is even faster than SetPixel because it does not return the color the pixel is set to.