取得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