Keith Maurino
Keith Maurino

Reputation: 3432

Convert a Picture Box image pixels to a Byte Array in VB6

I have a VB6 picture box that gets an image from a video capture device.

I'm trying to figure out how to then convert the picture box to a byte array.

Upvotes: 4

Views: 14876

Answers (2)

Keith Maurino
Keith Maurino

Reputation: 3432

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 Sub GetPictureBytes()
  Dim PicBits() As Byte, PicInfo As BITMAP

  GetObject Picture1.Picture, Len(PicInfo), PicInfo

  ReDim PicBits((PicInfo.bmWidth * PicInfo.bmHeight * 3) - 1) As Byte

  GetBitmapBits Picture1.Picture, UBound(PicBits), PicBits(0)
End Sub

Upvotes: 5

Konrad Rudolph
Konrad Rudolph

Reputation: 545508

It's been a long time since I've worked with VB6 but as far as I remember, you can just serialize the image into a PropertyBag and get the contents as a byte array.

The only alternative I know requires heavy use of WinAPI to accomplish the same.

Upvotes: 2

Related Questions