1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| Option Explicit
Public Enum CBoolean
cFalse = 0 ' 0&
cTrue ' 1&
End Enum
Private Const sIID_stdPicture = "{7BF80980-BF32-101A-8BBB-00AA00300CAB}"
Private Const GMEM_MOVEABLE = &H2
Private Const S_OK = &H0
Private Const NOERROR = 0
Private Type GUID ' 16 bytes (128 bits)
dwData1 As Long ' 4 bytes
wData2 As Integer ' 2 bytes
wData3 As Integer ' 2 bytes
abData4(7) As Byte ' 8 bytes, zero based
End Type
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 Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
Destination As Any, _
Source As Any, _
ByVal Length As Long)
Private Declare Function CreateStreamOnHGlobal Lib "ole32" _
(ByVal hGlobal As Long, _
ByVal fDeleteOnRelease As CBoolean, _
ppstm As Any) As Long
Private Declare Function CLSIDFromString Lib "ole32" ( _
ByVal lpsz As Any, _
pclsid As GUID) As Long
Private Declare Function OleLoadPicture Lib "olepro32" _
(pStream As Any, _
ByVal lSize As Long, _
ByVal fRunmode As CBoolean, _
riid As GUID, _
ppvObj As Any) As Long
Private Function PicFromBitsB(bData() As Byte) As StdPicture
On Error GoTo Errored
Dim lReturn As Long 'long return value
Dim lSize As Long 'long size of byte array
Dim hMem As Long 'handle to allocated memory
Dim lpMem As Long 'long pointer to allocated memory
Dim CLSID_stdPicture As GUID 'Class Identifier for stdPicture
Dim oIStream As stdole.IUnknown 'IStream Oject
'get data size
lSize = (UBound(bData) - LBound(bData)) + 1
If lSize = 0 Then
Set PicFromBitsB = Nothing
Exit Function
End If
'allocate global memory object and return handle
hMem = GlobalAlloc(GMEM_MOVEABLE, lSize)
If hMem = 0 Then GoTo Errored
'lock the memory by handle and return pointer to it
lpMem = GlobalLock(hMem)
If lpMem = 0 Then GoTo Errored
'copy the picture data to the memory and unlock the handle
CopyMemory ByVal lpMem, bData(LBound(bData)), lSize
Call GlobalUnlock(hMem)
'create an IStream object from the pic data
lReturn = CreateStreamOnHGlobal(hMem, cTrue, oIStream)
If lReturn <> S_OK Then GoTo Errored
'convert our stdPicture string to GUID
lReturn = CLSIDFromString(StrPtr(sIID_stdPicture), CLSID_stdPicture)
If lReturn <> NOERROR Then GoTo Errored
'create an stdPicture object from IStream and return PicFromBits as pointer
lReturn = OleLoadPicture(ByVal ObjPtr(oIStream), lSize, cFalse, CLSID_stdPicture, PicFromBitsB)
If lReturn <> S_OK Then GoTo Errored
Errored:
'clean up if needed
If hMem <> 0 Then GlobalFree (hMem)
End Function |
Partager