Results 1 to 8 of 8

Thread: BitMap to Bytes - An Attempt

Threaded View

  1. #1

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849

    Smile BitMap to Bytes - An Attempt

    Hi!

    A lot of times it has been asked, yours truly inlcuded, how a Bitmap in an app can be serialized into a byte array. Given below is an attempt modifying an example from All-API Guide. Hope this provides a start to all of you. The array is now written to disk. But can be sent directly through Winsock for network tranfer.

    VB Code:
    1. 'The Sending App, containing a PictureBox and a Command Button
    2. 'ScaleMode of the Form and the PictureBox set to Pixels
    3.  
    4. Private Const BI_RGB = 0&
    5. Private Const DIB_RGB_COLORS = 0
    6. Private Type BITMAPINFOHEADER
    7.         biSize As Long
    8.         biWidth As Long
    9.         biHeight As Long
    10.         biPlanes As Integer
    11.         biBitCount As Integer
    12.         biCompression As Long
    13.         biSizeImage As Long
    14.         biXPelsPerMeter As Long
    15.         biYPelsPerMeter As Long
    16.         biClrUsed As Long
    17.         biClrImportant As Long
    18. End Type
    19. Private Type RGBQUAD
    20.         rgbBlue As Byte
    21.         rgbGreen As Byte
    22.         rgbRed As Byte
    23.         rgbReserved As Byte
    24. End Type
    25. Private Type BITMAPINFO
    26.         bmiHeader As BITMAPINFOHEADER
    27.         bmiColors As RGBQUAD
    28. End Type
    29. Private Declare Function CreateCompatibleDC Lib "gdi32" _
    30. (ByVal hdc As Long) As Long
    31. Private Declare Function CreateDIBSection Lib "gdi32" _
    32. (ByVal hdc As Long, pBitmapInfo As BITMAPINFO, _
    33. ByVal un As Long, ByVal lplpVoid As Long, _
    34. ByVal handle As Long, ByVal dw As Long) As Long
    35. Private Declare Function GetDIBits Lib "gdi32" _
    36. (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan _
    37. As Long, ByVal nNumScans As Long, lpBits As Any, _
    38. lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
    39. Private Declare Function SelectObject Lib "gdi32" _
    40. (ByVal hdc As Long, ByVal hObject As Long) As Long
    41. Private Declare Function DeleteDC Lib "gdi32" _
    42. (ByVal hdc As Long) As Long
    43. Private Declare Function DeleteObject Lib "gdi32" _
    44. (ByVal hObject As Long) As Long
    45. Private Declare Function BitBlt Lib "gdi32" _
    46. (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, _
    47. ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC _
    48. As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
    49. ByVal dwRop As Long) As Long
    50. Private Declare Function GetDC Lib "user32" _
    51. (ByVal hwnd As Long) As Long
    52. Dim iBitmap As Long, iDC As Long
    53.  
    54. Private Sub Command1_Click()
    55.     Dim bi24BitInfo As BITMAPINFO, bBytes() As Byte
    56.     With bi24BitInfo.bmiHeader
    57.         .biBitCount = 24
    58.         .biCompression = BI_RGB
    59.         .biPlanes = 1
    60.         .biSize = Len(bi24BitInfo.bmiHeader)
    61.         .biWidth = Picture1.Width
    62.         .biHeight = Picture1.Height
    63.     End With
    64.     ReDim bBytes(1 To bi24BitInfo.bmiHeader.biWidth * _
    65.     bi24BitInfo.bmiHeader.biHeight * 3) As Byte
    66.  
    67.     iDC = CreateCompatibleDC(0)
    68.  
    69.     iBitmap = CreateDIBSection(iDC, bi24BitInfo, _
    70.     DIB_RGB_COLORS, ByVal 0&, ByVal 0&, ByVal 0&)
    71.  
    72.     SelectObject iDC, iBitmap
    73.  
    74.     BitBlt iDC, 0, 0, bi24BitInfo.bmiHeader.biWidth, _
    75.     bi24BitInfo.bmiHeader.biHeight, Picture1.hdc, 0, 0, _
    76.     vbSrcCopy
    77.  
    78.     GetDIBits iDC, iBitmap, 0, _
    79.     bi24BitInfo.bmiHeader.biHeight, bBytes(1), _
    80.     bi24BitInfo, DIB_RGB_COLORS
    81.  
    82.         Open App.Path & "\CloudsCopy.BMP" For Binary As #1
    83.             Put #1, 1, CLng(Picture1.Width)
    84.             Put #1, 5, CLng(Picture1.Height)
    85.             Put #1, 9, bBytes
    86.         Close #1
    87.     DeleteDC iDC
    88.     DeleteObject iBitmap
    89.     Client.Show 'Run the client App. I used two forms in the same app
    90. End Sub
    91.  
    92. Private Sub Form_Load()
    93. Me.ScaleMode = vbPixels
    94. With Picture1
    95.     .ScaleMode = vbPixels
    96.     .AutoRedraw = True
    97.     .AutoSize = True
    98.     .Picture = LoadPicture(App.Path & "\Clouds.bmp")
    99. End With
    100. End Sub
    101.  
    102. 'The recieving App containing a pictureBox with a command button
    103. 'with the form and picturebox scalemode set to Pixels and
    104. 'autoredraw, autosize of the pictureBox set to true
    105.  
    106. Private Declare Function SetDIBitsToDevice Lib "gdi32" _
    107. (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, _
    108. ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, _
    109. ByVal SrcY As Long, ByVal Scan As Long, ByVal NumScans _
    110. As Long, Bits As Any, BitsInfo As BITMAPINFO, ByVal wUsage _
    111. As Long) As Long
    112. Private Const BI_RGB = 0&
    113. Private Const DIB_RGB_COLORS = 0
    114. Private Type BITMAPINFOHEADER
    115.         biSize As Long
    116.         biWidth As Long
    117.         biHeight As Long
    118.         biPlanes As Integer
    119.         biBitCount As Integer
    120.         biCompression As Long
    121.         biSizeImage As Long
    122.         biXPelsPerMeter As Long
    123.         biYPelsPerMeter As Long
    124.         biClrUsed As Long
    125.         biClrImportant As Long
    126. End Type
    127. Private Type RGBQUAD
    128.         rgbBlue As Byte
    129.         rgbGreen As Byte
    130.         rgbRed As Byte
    131.         rgbReserved As Byte
    132. End Type
    133. Private Type BITMAPINFO
    134.         bmiHeader As BITMAPINFOHEADER
    135.         bmiColors As RGBQUAD
    136. End Type
    137.  
    138. Private Sub Command1_Click()
    139. Dim bi24BitInfo As BITMAPINFO, bmpWidth As Long, _
    140. bmpHeight As Long
    141. Dim bbBytes() As Byte
    142. Open App.Path & "\CloudsCopy.BMP" For Binary As #1
    143.     ReDim bbBytes(1 To (LOF(1) - 8))
    144.         Get #1, 1, bmpWidth
    145.         Get #1, 5, bmpHeight
    146.         Get #1, 9, bbBytes
    147. Close #1
    148.  
    149.     With bi24BitInfo.bmiHeader
    150.         .biBitCount = 24
    151.         .biCompression = BI_RGB
    152.         .biPlanes = 1
    153.         .biSize = Len(bi24BitInfo.bmiHeader)
    154.         .biWidth = bmpWidth
    155.         .biHeight = bmpHeight
    156.     End With
    157.         Picture1.Width = bmpWidth
    158.         Picture1.Height = bmpHeight
    159.    
    160.     SetDIBitsToDevice Picture1.hdc, 0, 0, _
    161.     bi24BitInfo.bmiHeader.biWidth, _
    162.     bi24BitInfo.bmiHeader.biHeight, 0, 0, 0, _
    163.     bi24BitInfo.bmiHeader.biHeight, bbBytes(1), _
    164.     bi24BitInfo, DIB_RGB_COLORS
    165.    
    166.     Picture1.AutoSize = True
    167.     Picture1.Refresh
    168. End Sub
    169.  
    170. Private Sub Form_Load()
    171. Me.ScaleMode = vbPixels
    172. Picture1.ScaleMode = vbPixels
    173. End Sub
    Last edited by KayJay; Oct 5th, 2002 at 05:11 AM.

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width