取得PictureBox内BitMap图的各个Byte

来源:cww

'Form 中有PictureBox并指定好图形,另一CommandBox

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 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 Sub Command1_Click()
Dim hBitmap As Long
Dim res As Long
Dim bmp As BITMAP
Dim byteAry() As Byte
Dim totbyte As Long, i As Long
hBitmap = Picture1.Picture.Handle

res = GetObject(hBitmap, Len(bmp), bmp) '取得BitMap的结构

totbyte = bmp.bmWidthBytes * bmp.bmHeight '总共要多少个Byte来存图

ReDim byteAry(totbyte - 1)
'将该图全放进ByteAry中
res = GetBitmapBits(hBitmap, totbyte, byteAry(0))

Debug.Print "Total Bytes Copied :"; res
Debug.Print "bmp.bmBits "; bmp.bmBits
Debug.Print "bmp.bmBitsPixel "; bmp.bmBitsPixel '每个Pixel需多少Bits来表现
Debug.Print "bmp.bmHeight "; bmp.bmHeight       'bitmap图的高是多少Pixels
Debug.Print "bmp.bmPlanes "; bmp.bmPlanes
Debug.Print "bmp.bmType "; bmp.bmType            
Debug.Print "bmp.bmWidth "; bmp.bmWidth         'BitMap图宽为多少pixels     
Debug.Print "bmp.bmWidthBytes "; bmp.bmWidthBytes  '每条扫描线需多少Byte来存
End Sub