Image <-> Byte Array



Hi all,

I have the following 2 functions:

1. ConvertToPicture: converts a byte array to a picture
2. ConvertToByteArray: converts a picture to a byte array

In ConvertToByteArray, there are a few lines of test code (commented):
When I save the original oImage to a file, it's about 3.5MB and is a valid
bmp. But when I convert the byte array back to an image, the temporary
'pic.tmp' file is 11.5MB and is not a valid bmp.

Any ideas?

Thanks,
Ivan

Public Function ConvertToPicture(aImage() As Byte) As IPictureDisp
On Error GoTo trap_err

Dim iFile As Integer
Dim sFile As String

If UBound(aImage) > 0 Then
sFile = App.Path & "\pic.tmp"
iFile = FreeFile()
Open sFile For Binary As #iFile
Put #iFile, , aImage()
Close #iFile

Set ConvertToPicture = LoadPicture(sFile)

If Dir(sFile) <> vbNullString Then
Kill sFile
End If
Else
'Return nothing
Set ConvertToPicture = Nothing
End If

trap_err_exit:
Exit Function

trap_err:
Resume trap_err_exit

End Function

Public Function ConvertToByteArray(ByVal oImage As IPictureDisp) As Byte()
Dim PicBits() As Byte
Dim PicInfo As BITMAP
Dim Cnt As Long
Dim BytesPerLine As Long

GetObject oImage, Len(PicInfo), PicInfo

BytesPerLine = (PicInfo.bmWidth * 3 + 3) And &HFFFFFFFC
ReDim PicBits(1 To BytesPerLine * PicInfo.bmHeight * 3) As Byte

GetBitmapBits oImage, UBound(PicBits), PicBits(1)
For Cnt = 1 To UBound(PicBits)
PicBits(Cnt) = 255 - PicBits(Cnt)
Next Cnt

ConvertToByteArray = PicBits

'Test to see if oimage saves a valid bmp
SavePicture oImage, App.Path & "\test.bmp"

'Test to see if o is a valid bmp
Dim o As Object
Set o = ConvertToPicture(PicBits)
SavePicture o, App.Path & "\test2.bmp"
End Function


.