Option Explicit
Private Const SIPICTURE As String = "{7BF80980-BF32-101A-8BBB-00AA00300CAB}"
Private Const GMEM_MOVEABLE As Long = &H2
Private Const GMEM_ZEROINIT As Long = &H40
Private Const NOERROR As Long = 0
Private Const S_OK As Long = 0
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Declare Function CLSIDFromString Lib "ole32" (ByVal lpsz As Long, ByRef pclsid As GUID) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function CreateStreamOnHGlobal Lib "ole32" (ByVal hGlobal As Long, ByVal fDeleteOnRelease As Long, pPStm As Any) As Long
Private Declare Function OleLoadPicture Lib "olepro32.dll" (ByVal lpStream As Long, ByVal lSize As Long, ByVal fRunMode As Long, ByRef riid As GUID, ByRef lplpObj As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal length As Long)
Public Function BinaryDataToStdPicture(ByRef binaryData() As Byte) As StdPicture
Dim hMem As Long
Dim memPtr As Long
Dim stm As IUnknown
Dim stdPicture_GUID As GUID
Dim length As Long
Dim pic As IPicture
length = UBound(binaryData) + 1
hMem = GlobalAlloc((GMEM_MOVEABLE Or GMEM_ZEROINIT), length)
If hMem = 0 Then Exit Function
memPtr = GlobalLock(hMem)
If memPtr <> 0 Then
CopyMemory ByVal memPtr, binaryData(0), length
If GlobalUnlock(hMem) = S_OK Then
If CreateStreamOnHGlobal(hMem, False, stm) = S_OK Then
If CLSIDFromString(StrPtr(SIPICTURE), stdPicture_GUID) = NOERROR Then
If OleLoadPicture(ByVal ObjPtr(stm), 0&, 0&, stdPicture_GUID, pic) = S_OK Then
Set BinaryDataToStdPicture = pic
End If
End If
End If
End If
End If
GlobalFree hMem
End Function