'Create a new project, add a command button and a picture box to the project, load a picture into the picture box. 'Paste this code into Form1 Private Type BITMAP bmType As Long bmWidth As Long bmHeight As Long bmWidthBytes As Long bmPlanes As Integer bmBitsPixel As Integer bmBits As Long End Type Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long Dim PicBits() As Byte, PicInfo As BITMAP, Cnt As Long Private Sub Command1_Click() 'Get information (such as height and width) about the picturebox GetObject Picture1.Image, Len(PicInfo), PicInfo 'reallocate storage space ReDim PicBits(1 To PicInfo.bmWidthBytes * PicInfo.bmHeight) As Byte '取出颜色信息到数组PicBits GetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1) '颜色反转 For Cnt = 1 To UBound(PicBits) PicBits(Cnt) = 255 - PicBits(Cnt) Next Cnt '传回picture SetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1) 'refresh Picture1.Refresh End Sub
问题解决了,不过有个疑问 msdn 对 GetBitmapBits 有这么一句话Note This function is provided only for compatibility with 16-bit versions of Windows. Win32-based applications should use the GetDIBits function. 也就是说GetBitmapBits只适合在16-bit的win下工作,而我在win2k下(这个应该是32-bit的win吧),依然能正常,何解? 是否应该使用GetDIBits?
'Paste this code into Form1
Private Type BITMAP
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Dim PicBits() As Byte, PicInfo As BITMAP, Cnt As Long
Private Sub Command1_Click()
'Get information (such as height and width) about the picturebox
GetObject Picture1.Image, Len(PicInfo), PicInfo
'reallocate storage space
ReDim PicBits(1 To PicInfo.bmWidthBytes * PicInfo.bmHeight) As Byte
'取出颜色信息到数组PicBits
GetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1)
'颜色反转
For Cnt = 1 To UBound(PicBits)
PicBits(Cnt) = 255 - PicBits(Cnt)
Next Cnt
'传回picture
SetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1)
'refresh
Picture1.Refresh
End Sub
msdn 对 GetBitmapBits 有这么一句话Note This function is provided only for compatibility with 16-bit versions of Windows. Win32-based applications should use the GetDIBits function. 也就是说GetBitmapBits只适合在16-bit的win下工作,而我在win2k下(这个应该是32-bit的win吧),依然能正常,何解?
是否应该使用GetDIBits?