从VB6中的字节数组加载图片框图像

时间:2014-10-18 17:37:03

标签: vb6 picturebox

问题here很好地解释了如何将图片框图像转换为VB6中的字节数组。 我想反过来并从字节数组加载我的图片框图像。

我找到了部分解决方案 here,它使用以下代码

Public Function ArrayToPicture(inArray() As Byte, Offset As Long, Size As Long) As IPicture

    ' function creates a stdPicture from the passed array
    ' Offset is first item in array: 0 for 0 bound arrays
    ' Size is how many bytes comprise the image
    Dim o_hMem  As Long
    Dim o_lpMem  As Long
    Dim aGUID(0 To 3) As Long
    Dim IIStream As IUnknown

    aGUID(0) = &H7BF80980    ' GUID for stdPicture
    aGUID(1) = &H101ABF32
    aGUID(2) = &HAA00BB8B
    aGUID(3) = &HAB0C3000

    o_hMem = GlobalAlloc(&H2&, Size)
    If Not o_hMem = 0& Then
        o_lpMem = GlobalLock(o_hMem)
        If Not o_lpMem = 0& Then
            CopyMemory ByVal o_lpMem, inArray(Offset), Size
            Call GlobalUnlock(o_hMem)
            If CreateStreamOnHGlobal(o_hMem, 1&, IIStream) = 0& Then
                  Call OleLoadPicture(ByVal ObjPtr(IIStream), 0&, 0&, aGUID(0), ArrayToPicture)
            End If
        End If
    End If
End Function

如何让Offset和Size传递给这个函数?

1 个答案:

答案 0 :(得分:1)

Size参数是构成数组中图像的总字节数,Offset是数据开始的数组的索引,这允许单个数组存储多个图像。

如果数组仅包含单个图片,请为LBound传递OffsetUBound - LBound + 1传递Size