'代码出处:http://www.360doc.com/content/08/0505/17/15897_1238189.shtml
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Function Red(ByVal mlColor As Long) As Long
'从RGB值中获得红色值
Red = mlColor And &HFF
End Function
Private Function Green(ByVal mlColor As Long) As Long
'从RGB值中获得绿色值
Green = (mlColor \ &H100) And &HFF
End Function
Private Function Blue(ByVal mlColor As Long) As Long
''从RGB值中获得蓝色值
Blue = (mlColor \ &H10000) And &HFF
End Function
Private Sub Command1_click()
Picture1.ScaleMode = 3
Picture1.AutoRedraw = True
Dim width5 As Long, heigh5 As Long, rgb5 As Long
Dim hdc5 As Long, i As Long, j As Long
Dim bBlue As Long, bRed As Long, bGreen As Long
Dim y As Long
width5 = Picture1.ScaleWidth
heigh5 = Picture1.ScaleHeight
hdc5 = Picture1.hdc
For i = 1 To width5
For j = 1 To heigh5
rgb5 = GetPixel(hdc5, i, j)
bBlue = Blue(rgb5) '获得蓝色值
bRed = Red(rgb5) '获得红色值
bGreen = Green(rgb5) '获得绿色值
y = (9798 * bRed + 19235 * bGreen + 3735 * bBlue) \ 32768 '将三原色转换为灰度
If y > 127 Then '127为阈值,请按实际请况自行调整
y = 255
Else
y = 0
End If
rgb5 = RGB(y, y, y)
SetPixelV hdc5, i, j, rgb5
Next j
Next i
Set Picture1.Picture = Picture1.Image
End Sub