一个GetBitmapBits的例子,将picturebox中图片颜色反转 '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.bmWidth * PicInfo.bmWidthBytes) As Byte 'Copy the bitmapbits to the array GetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1) 'Invert the bits For Cnt = 1 To UBound(PicBits) PicBits(Cnt) = 255 - PicBits(Cnt) Next Cnt 'Set the bits back to the picture SetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1) 'refresh Picture1.Refresh End Sub
注意:楼上的代码仅对Invert效果有效,因为PictureBox是DDB的
'======================================================================= Private Type BITMAP '14 bytes 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 Type BITMAPFILEHEADER '位图头信息 bfType(0 To 1) As Byte '位图的类型 bfSize As Long '用字节表示的整个文件的大小 bfReserved1 As Integer '保留,必须设置为0 bfReserved2 As Integer '保留,必须设置为0 bfOffBits As Long '从文件开始到位图数据开始之间的数据(bitmap data)之间的偏移量 End Type Private Type BITMAPINFOHEADER '40 bytes '位图信息头 biSize As Long 'BITMAPINFOHEADER结构所需要的字数 biWidth As Long '图象的宽度,以象素为单位 biHeight As Long '图象的高度,以象素为单位 该值是一个正数,说明图像是倒向的,如果该值是一个负数,则说明图像是正向的 biPlanes As Integer '位面数,其值将总是被设为1 biBitCount As Integer '比特数/象素,其值为1、4、8、16、24、或32 biCompression As Long 'BI_RGB:没有压缩 'BI_RLE8:每个象素8比特的RLE压缩编码,压缩格式由2字节组成(重复象素计数和颜色索引); 'BI_RLE4:每个象素4比特的RLE压缩编码,压缩格式由2字节组成 'BI_BITFIELDS:每个象素的比特由指定的掩码决定。 biSizeImage As Long '图象的大小,以字节为单位。当用BI_RGB格式时,可设置为0 biXPelsPerMeter As Long '水平分辨率,用象素/米表示 biYPelsPerMeter As Long '垂直分辨率,用象素/米表示 biClrUsed As Long '位图实际使用的彩色表中的颜色索引数(设为0的话,则说明使用所有调色板项) biClrImportant As Long '图象显示有重要影响的颜色索引的数目,如果是0,表示都重要。 End Type Private Const BI_RGB = 0& Private Const BI_RLE8 = 1& Private Const BI_RLE4 = 2& Private Const BI_bitfields = 3&Private Type RGBQUAD '位图颜色数据 rgbBlue As Byte rgbGreen As Byte rgbRed As Byte rgbReserved As Byte End Type 这是BMP头文件的数据结构,需要详细的交流,可以给我Email:[email protected]
正解:
用GetDIBits或者GetBitmapBits函数,如果知道BMP文件格式的话,直接读取更好
'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.bmWidth * PicInfo.bmWidthBytes) As Byte
'Copy the bitmapbits to the array
GetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1)
'Invert the bits
For Cnt = 1 To UBound(PicBits)
PicBits(Cnt) = 255 - PicBits(Cnt)
Next Cnt
'Set the bits back to the picture
SetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1)
'refresh
Picture1.Refresh
End Sub
Private Type BITMAP '14 bytes
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 Type BITMAPFILEHEADER '位图头信息
bfType(0 To 1) As Byte '位图的类型
bfSize As Long '用字节表示的整个文件的大小
bfReserved1 As Integer '保留,必须设置为0
bfReserved2 As Integer '保留,必须设置为0
bfOffBits As Long '从文件开始到位图数据开始之间的数据(bitmap data)之间的偏移量
End Type
Private Type BITMAPINFOHEADER '40 bytes '位图信息头
biSize As Long 'BITMAPINFOHEADER结构所需要的字数
biWidth As Long '图象的宽度,以象素为单位
biHeight As Long '图象的高度,以象素为单位 该值是一个正数,说明图像是倒向的,如果该值是一个负数,则说明图像是正向的
biPlanes As Integer '位面数,其值将总是被设为1
biBitCount As Integer '比特数/象素,其值为1、4、8、16、24、或32
biCompression As Long 'BI_RGB:没有压缩
'BI_RLE8:每个象素8比特的RLE压缩编码,压缩格式由2字节组成(重复象素计数和颜色索引);
'BI_RLE4:每个象素4比特的RLE压缩编码,压缩格式由2字节组成
'BI_BITFIELDS:每个象素的比特由指定的掩码决定。
biSizeImage As Long '图象的大小,以字节为单位。当用BI_RGB格式时,可设置为0
biXPelsPerMeter As Long '水平分辨率,用象素/米表示
biYPelsPerMeter As Long '垂直分辨率,用象素/米表示
biClrUsed As Long '位图实际使用的彩色表中的颜色索引数(设为0的话,则说明使用所有调色板项)
biClrImportant As Long '图象显示有重要影响的颜色索引的数目,如果是0,表示都重要。
End Type
Private Const BI_RGB = 0&
Private Const BI_RLE8 = 1&
Private Const BI_RLE4 = 2&
Private Const BI_bitfields = 3&Private Type RGBQUAD '位图颜色数据
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
这是BMP头文件的数据结构,需要详细的交流,可以给我Email:[email protected]