Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21238

How to convert a byte array to image ?

$
0
0
Hello all,
I have a small function API based that allow us to transfer a bmp image, in a easy way, to a byte array. This array could be stored somewhere via PropertyBag and called whenever we need. Now, the reverse process would be to convert that byte array to image. I saw that this function is integrated in .NET and this conversion requires a few lines of code but I wonder how to do that in VB6.
I write below this function which many of you probably already know it :
Code:

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 Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long

Private Function GetPictureBytes(myImg As PictureBox) As Byte()
        Dim PicBits() As Byte, PicInfo As BITMAP
        'Get information (such as height and width) about the picturebox
        Call GetObject(myImg.Image, Len(PicInfo), PicInfo)
        'Reallocate storage space:  a pixel needs 4 bytes
        ReDim PicBits(1 To PicInfo.bmWidth * PicInfo.bmHeight * 4) As Byte
        'Copy the bitmapbits to the array
        Call GetBitmapBits(myImg.Image, UBound(PicBits), PicBits(1))
        Call SetBitmapBits(Picture2.Image, UBound(PicBits), PicBits(1))
        GetPictureBytes = PicBits
End Function


Viewing all articles
Browse latest Browse all 21238

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>