当我们用
GetPixel() 这样的函数来获取像素点信息时会返回一个长整数类型的值,若需要获得其以 RGB 形式的表示则需要进行转化,其在 VB 中转化的方法如下:
注:以上函数本质上为 RGB() 函数的反函数若调用时出现错误则返回-1,另外好像 C/C++ 中存在这样的转换函数在此不做 C/C++ 代码的讨论。
Public Function GetRValue(ByVal crColor As Long) As Integer
On Error GoTo ErrHandle
If crColor >= 0 Then
GetRValue = crColor Mod 256
Else
GetRValue = -1
End If
Exit Function
ErrHandle:
GetRValue = -1
End Function
Public Function GetGValue(ByVal crColor As Long) As Integer
On Error GoTo ErrHandle
If crColor >= 0 Then
GetGValue = Fix((crColor / 256)) Mod 256
Else
GetGValue = -1
End If
Exit Function
ErrHandle:
GetGValue = -1
End Function
Public Function GetBValue(ByVal crColor As Long) As Integer
On Error GoTo ErrHandle
If crColor >= 0 Then
GetBValue = Fix(crColor / 256 / 256)
Else
GetBValue = -1
End If
Exit Function
ErrHandle:
GetBValue = -1
End Function
On Error GoTo ErrHandle
If crColor >= 0 Then
GetRValue = crColor Mod 256
Else
GetRValue = -1
End If
Exit Function
ErrHandle:
GetRValue = -1
End Function
Public Function GetGValue(ByVal crColor As Long) As Integer
On Error GoTo ErrHandle
If crColor >= 0 Then
GetGValue = Fix((crColor / 256)) Mod 256
Else
GetGValue = -1
End If
Exit Function
ErrHandle:
GetGValue = -1
End Function
Public Function GetBValue(ByVal crColor As Long) As Integer
On Error GoTo ErrHandle
If crColor >= 0 Then
GetBValue = Fix(crColor / 256 / 256)
Else
GetBValue = -1
End If
Exit Function
ErrHandle:
GetBValue = -1
End Function