'将RGB想成3D之X,Y,Z轴,则BMP的RGB为(r,g,b)与座标(Y,Y,Y)距离最小时的Y即为灰度值 'Y = 0.299 * R + 0.587 * G + 0.114 * B ' 整数化 'Y = (9798 * R + 19235 * G + 3735 * B) / 32768 'RGB(Y, Y, Y)就可以了'需一个内有彩色图的PictureBox , CommandBoxOption Explicit 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 tmpPic As PicturePrivate Sub Form_Load() Picture1.ScaleMode = 3 '设为Pixel Picture1.AutoRedraw = True '设定所有Pixel的改变不立即在pictureBox上显示 Set tmpPic = Picture1.Picture End SubPrivate Sub Command1_click() 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 Longwidth5 = 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 rgb5 = RGB(Y, Y, Y) SetPixelV hdc5, i, j, rgb5 Next j Next i Set Picture1.Picture = Picture1.Image '此时才真正显示Picture End SubPrivate Function Red(ByVal mlColor As Long) As Long Red = mlColor And &HFF End Function Private Function Green(ByVal mlColor As Long) As Long Green = (mlColor \ &H100) And &HFF End Function Private Function Blue(ByVal mlColor As Long) As Long Blue = (mlColor \ &H10000) And &HFF End Function
Option Explicit ' Corrected Draw State function declarations: Private Declare Function DrawState Lib "user32" Alias "DrawStateA" _ (ByVal hdc As Long, _ ByVal hBrush As Long, _ ByVal lpDrawStateProc As Long, _ ByVal lParam As Long, _ ByVal wParam As Long, _ ByVal X As Long, _ ByVal Y As Long, _ ByVal cX As Long, _ ByVal cY As Long, _ ByVal fuFlags As Long) As Long Private Declare Function DrawStateString Lib "user32" Alias "DrawStateA" _ (ByVal hdc As Long, _ ByVal hBrush As Long, _ ByVal lpDrawStateProc As Long, _ ByVal lpString As String, _ ByVal cbStringLen As Long, _ ByVal X As Long, _ ByVal Y As Long, _ ByVal cX As Long, _ ByVal cY As Long, _ ByVal fuFlags As Long) As Long' Missing Draw State constants declarations: '/* Image type */ Private Const DST_COMPLEX = &H0 Private Const DST_TEXT = &H1 Private Const DST_PREFIXTEXT = &H2 Private Const DST_ICON = &H3 Private Const DST_BITMAP = &H4' /* State type */ Private Const DSS_NORMAL = &H0 Private Const DSS_UNION = &H10 Private Const DSS_DISABLED = &H20 Private Const DSS_MONO = &H80 Private Const DSS_RIGHT = &H8000' Create a new icon based on an image list icon: Private Declare Function ImageList_GetIcon Lib "COMCTL32.DLL" ( _ ByVal himl As Long, _ ByVal i As Long, _ ByVal diIgnore As Long _ ) As Long ' Draw an item in an ImageList: Private Declare Function ImageList_Draw Lib "COMCTL32.DLL" ( _ ByVal himl As Long, _ ByVal i As Long, _ ByVal hdcDst As Long, _ ByVal X As Long, _ ByVal Y As Long, _ ByVal fStyle As Long _ ) As Long ' Draw an item in an ImageList with more control over positioning ' and colour: Private Declare Function ImageList_DrawEx Lib "COMCTL32.DLL" ( _ ByVal himl As Long, _ ByVal i As Long, _ ByVal hdcDst As Long, _ ByVal X As Long, _ ByVal Y As Long, _ ByVal dx As Long, _ ByVal dy As Long, _ ByVal rgbBk As Long, _ ByVal rgbFg As Long, _ ByVal fStyle As Long _ ) As Long ' Built in ImageList drawing methods: Private Const ILD_NORMAL = 0 Private Const ILD_TRANSPARENT = 1 Private Const ILD_BLEND25 = 2 Private Const ILD_SELECTED = 4 Private Const ILD_FOCUS = 4 Private Const ILD_OVERLAYMASK = 3840 ' Use default rgb colour: Private Const CLR_NONE = -1' Standard GDI draw icon function: Private Declare Function DrawIconEx Lib "user32" (ByVal hdc As Long, ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As Long, ByVal cyWidth As Long, ByVal istepIfAniCur As Long, ByVal hbrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long Private Const DI_MASK = &H1 Private Const DI_IMAGE = &H2 Private Const DI_NORMAL = &H3 Private Const DI_COMPAT = &H4 Private Const DI_DEFAULTSIZE = &H8' Clear up GDI object: Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long ' Create a GDI brush: Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long ' Get a System Colour (can use OLETranslateColor instead for this): Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long Private Const COLOR_WINDOW = 5Private Sub Draw() Dim himl As Long Dim hIcon As Long Dim i As Long Dim lR As Long Dim hBr As Long Dim lFlags As Long ' Create a green brush to colourise items in DrawText hBr = CreateSolidBrush(&HFF00&)
' Draw the headers using the DrawState function: DrawStateString Me.hdc, 0, 0, "Normal", Len("Normal"), 25, 4, 50, 13, DST_TEXT Or DSS_NORMAL DrawStateString Me.hdc, 0, 0, "Disabled", Len("Disabled"), 75, 4, 50, 13, DST_TEXT Or DSS_DISABLED ' DrawStateString Me.hdc, hBr, 0, "Coloured", Len("Coloured"), 125, 4, 50, 13, DST_TEXT Or DSS_MONO ' DrawStateString Me.hdc, 0, 0, "Dithered", Len("Dithered"), 175, 4, 50, 13, DST_TEXT Or DSS_UNION ' ' DrawStateString Me.hdc, 0, 0, "Selected", Len("Selected"), 225, 4, 50, 13, DST_TEXT Or DSS_NORMAL ' DrawStateString Me.hdc, 0, 0, "'Cut'", Len("'Cut'"), 275, 4, 50, 13, DST_TEXT Or DSS_NORMAL ' Get the image list handle (note we do draw an item first to ensure it ' has been initialised, otherwise the code can error): ilsIcons.ListImages(1).Draw 0, 0, 0 himl = ilsIcons.hImageList For i = 1 To ilsIcons.ListImages.Count ' Get an icon from the image list: hIcon = ImageList_GetIcon(himl, i - 1, 0) ' Draw it in standard state: DrawIconEx Me.hdc, 25, i * 20, hIcon, 16, 16, 0, 0, DI_NORMAL ' Now demonstrate the DrawState effects: lR = DrawState(Me.hdc, 0, 0, hIcon, 0, 75, i * 20, 16, 16, DST_ICON Or DSS_DISABLED) 'lR = DrawState(Me.hdc, hBr, 0, hIcon, 0, 125, i * 20, 16, 16, DST_ICON Or DSS_MONO) ' Note DSS_UNION (dithering) only seems to work in NT: 'lR = DrawState(Me.hdc, 0, 0, hIcon, 0, 175, i * 20, 16, 16, DST_ICON Or DSS_UNION) ' Clear up the Icon object: DeleteObject hIcon ' Image list drawing commands: 'lFlags = ILD_TRANSPARENT Or ILD_SELECTED ' Draw the icon in the selected state: ' lR = ImageList_Draw(himl, i - 1, Me.hdc, 225, i * 20, lFlags) ' Draw the icon in the selected state, but set the dithering colour to ' the Window background colour rather than the highlight colour to ' give the effect seen when you Cut a file in Explorer: ' lR = ImageList_DrawEx(himl, i - 1, picCut.hdc, 0, (i - 1) * 20, 0, 0, CLR_NONE, GetSysColor(COLOR_WINDOW), lFlags) Next i
' Clear up the Brush used to colourise the icon: DeleteObject hBr
' Show the cut icons (these are only drawn on a picture box so the ' background is the correct colour here): picCut.Refresh
End SubPrivate Sub Form_Paint() ' Refresh the form: Draw End Sub
'Y = 0.299 * R + 0.587 * G + 0.114 * B
' 整数化
'Y = (9798 * R + 19235 * G + 3735 * B) / 32768
'RGB(Y, Y, Y)就可以了'需一个内有彩色图的PictureBox , CommandBoxOption Explicit
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 tmpPic As PicturePrivate Sub Form_Load()
Picture1.ScaleMode = 3 '设为Pixel
Picture1.AutoRedraw = True '设定所有Pixel的改变不立即在pictureBox上显示
Set tmpPic = Picture1.Picture
End SubPrivate Sub Command1_click()
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 Longwidth5 = 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
rgb5 = RGB(Y, Y, Y)
SetPixelV hdc5, i, j, rgb5
Next j
Next i
Set Picture1.Picture = Picture1.Image '此时才真正显示Picture
End SubPrivate Function Red(ByVal mlColor As Long) As Long
Red = mlColor And &HFF
End Function
Private Function Green(ByVal mlColor As Long) As Long
Green = (mlColor \ &H100) And &HFF
End Function
Private Function Blue(ByVal mlColor As Long) As Long
Blue = (mlColor \ &H10000) And &HFF
End Function
rgb(255,255,255),即黑色的转换后还是rgb(255,255,255)所以你可变通一下,如果是白色rgb(255,255,255)可变为(200,200,200)或其它数字
黑色rgb(255,255,255)可变为(100,100,100)或其它数字
这样黑的不黑,白的不白,当然是灰了:)
' Corrected Draw State function declarations:
Private Declare Function DrawState Lib "user32" Alias "DrawStateA" _
(ByVal hdc As Long, _
ByVal hBrush As Long, _
ByVal lpDrawStateProc As Long, _
ByVal lParam As Long, _
ByVal wParam As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal cX As Long, _
ByVal cY As Long, _
ByVal fuFlags As Long) As Long
Private Declare Function DrawStateString Lib "user32" Alias "DrawStateA" _
(ByVal hdc As Long, _
ByVal hBrush As Long, _
ByVal lpDrawStateProc As Long, _
ByVal lpString As String, _
ByVal cbStringLen As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal cX As Long, _
ByVal cY As Long, _
ByVal fuFlags As Long) As Long' Missing Draw State constants declarations:
'/* Image type */
Private Const DST_COMPLEX = &H0
Private Const DST_TEXT = &H1
Private Const DST_PREFIXTEXT = &H2
Private Const DST_ICON = &H3
Private Const DST_BITMAP = &H4' /* State type */
Private Const DSS_NORMAL = &H0
Private Const DSS_UNION = &H10
Private Const DSS_DISABLED = &H20
Private Const DSS_MONO = &H80
Private Const DSS_RIGHT = &H8000' Create a new icon based on an image list icon:
Private Declare Function ImageList_GetIcon Lib "COMCTL32.DLL" ( _
ByVal himl As Long, _
ByVal i As Long, _
ByVal diIgnore As Long _
) As Long
' Draw an item in an ImageList:
Private Declare Function ImageList_Draw Lib "COMCTL32.DLL" ( _
ByVal himl As Long, _
ByVal i As Long, _
ByVal hdcDst As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal fStyle As Long _
) As Long
' Draw an item in an ImageList with more control over positioning
' and colour:
Private Declare Function ImageList_DrawEx Lib "COMCTL32.DLL" ( _
ByVal himl As Long, _
ByVal i As Long, _
ByVal hdcDst As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal dx As Long, _
ByVal dy As Long, _
ByVal rgbBk As Long, _
ByVal rgbFg As Long, _
ByVal fStyle As Long _
) As Long
' Built in ImageList drawing methods:
Private Const ILD_NORMAL = 0
Private Const ILD_TRANSPARENT = 1
Private Const ILD_BLEND25 = 2
Private Const ILD_SELECTED = 4
Private Const ILD_FOCUS = 4
Private Const ILD_OVERLAYMASK = 3840
' Use default rgb colour:
Private Const CLR_NONE = -1' Standard GDI draw icon function:
Private Declare Function DrawIconEx Lib "user32" (ByVal hdc As Long, ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As Long, ByVal cyWidth As Long, ByVal istepIfAniCur As Long, ByVal hbrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long
Private Const DI_MASK = &H1
Private Const DI_IMAGE = &H2
Private Const DI_NORMAL = &H3
Private Const DI_COMPAT = &H4
Private Const DI_DEFAULTSIZE = &H8' Clear up GDI object:
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
' Create a GDI brush:
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
' Get a System Colour (can use OLETranslateColor instead for this):
Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
Private Const COLOR_WINDOW = 5Private Sub Draw()
Dim himl As Long
Dim hIcon As Long
Dim i As Long
Dim lR As Long
Dim hBr As Long
Dim lFlags As Long ' Create a green brush to colourise items in DrawText
hBr = CreateSolidBrush(&HFF00&)
' Draw the headers using the DrawState function:
DrawStateString Me.hdc, 0, 0, "Normal", Len("Normal"), 25, 4, 50, 13, DST_TEXT Or DSS_NORMAL
DrawStateString Me.hdc, 0, 0, "Disabled", Len("Disabled"), 75, 4, 50, 13, DST_TEXT Or DSS_DISABLED
' DrawStateString Me.hdc, hBr, 0, "Coloured", Len("Coloured"), 125, 4, 50, 13, DST_TEXT Or DSS_MONO
' DrawStateString Me.hdc, 0, 0, "Dithered", Len("Dithered"), 175, 4, 50, 13, DST_TEXT Or DSS_UNION
'
' DrawStateString Me.hdc, 0, 0, "Selected", Len("Selected"), 225, 4, 50, 13, DST_TEXT Or DSS_NORMAL
' DrawStateString Me.hdc, 0, 0, "'Cut'", Len("'Cut'"), 275, 4, 50, 13, DST_TEXT Or DSS_NORMAL ' Get the image list handle (note we do draw an item first to ensure it
' has been initialised, otherwise the code can error):
ilsIcons.ListImages(1).Draw 0, 0, 0
himl = ilsIcons.hImageList
For i = 1 To ilsIcons.ListImages.Count
' Get an icon from the image list:
hIcon = ImageList_GetIcon(himl, i - 1, 0)
' Draw it in standard state:
DrawIconEx Me.hdc, 25, i * 20, hIcon, 16, 16, 0, 0, DI_NORMAL
' Now demonstrate the DrawState effects:
lR = DrawState(Me.hdc, 0, 0, hIcon, 0, 75, i * 20, 16, 16, DST_ICON Or DSS_DISABLED)
'lR = DrawState(Me.hdc, hBr, 0, hIcon, 0, 125, i * 20, 16, 16, DST_ICON Or DSS_MONO)
' Note DSS_UNION (dithering) only seems to work in NT:
'lR = DrawState(Me.hdc, 0, 0, hIcon, 0, 175, i * 20, 16, 16, DST_ICON Or DSS_UNION)
' Clear up the Icon object:
DeleteObject hIcon
' Image list drawing commands:
'lFlags = ILD_TRANSPARENT Or ILD_SELECTED
' Draw the icon in the selected state:
' lR = ImageList_Draw(himl, i - 1, Me.hdc, 225, i * 20, lFlags)
' Draw the icon in the selected state, but set the dithering colour to
' the Window background colour rather than the highlight colour to
' give the effect seen when you Cut a file in Explorer:
' lR = ImageList_DrawEx(himl, i - 1, picCut.hdc, 0, (i - 1) * 20, 0, 0, CLR_NONE, GetSysColor(COLOR_WINDOW), lFlags) Next i
' Clear up the Brush used to colourise the icon:
DeleteObject hBr
' Show the cut icons (these are only drawn on a picture box so the
' background is the correct colour here):
picCut.Refresh
End SubPrivate Sub Form_Paint()
' Refresh the form:
Draw
End Sub