新手刚来:我有一个jpg图在一个byte数组内请问如何用PictureBox显示
我有一个jpg图在一个byte数组内请问如何用PictureBox显示,最好能有关键代码
[解决办法]
Public Declare Function CreateStreamOnHGlobal Lib "ole32 " (ByVal hGlobal As Long, ByVal fDeleteOnRelease As Long, ppstm As Any) As Long
Public Declare Function OleLoadPicture Lib "olepro32 " (pStream As Any, ByVal lSize As Long, ByVal fRunmode As Long, riid As Any, ppvObj As Any) As Long
Public Declare Function CLSIDFromString Lib "ole32 " (ByVal lpsz As Any, pclsid As Any) As Long
Public Declare Function GlobalAlloc Lib "kernel32 " (ByVal uFlags As Long, ByVal dwBytes As Long) As Long
Public Declare Function GlobalLock Lib "kernel32 " (ByVal hMem As Long) As Long
Public Declare Function GlobalUnlock Lib "kernel32 " (ByVal hMem As Long) As Long
Public Declare Function GlobalFree Lib "kernel32 " (ByVal hMem As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32 " Alias "RtlMoveMemory " (Destination As Any, Source As Any, ByVal Length As Long)
'**************************************************************
' 参数说明:
'
' bImageData(): 保存图像信息的字节数组。
'
' 返回值:
'
' 返回转换后的 IPictreDisp 对象。
'**************************************************************
Public Function GetPictureFromByteStream(bImageData() As Byte) As IPicture
Dim lngByteCount As Long
Dim hMem As Long
Dim lpMem As Long
Dim IID_IPicture(15)
Dim IStream As stdole.IUnknown
On Error GoTo Err_Init
lngByteCount = UBound(bImageData) + 1 ' 计算数组大小
hMem = GlobalAlloc(&H2 Or GMEM_ZEROINIT, lngByteCount) ' 按数组大小分配一块内存空间
If hMem <> 0 Then
' ' 若分配内存成功
lpMem = GlobalLock(hMem) ' 锁定内存, 返回第一块的指针
If lpMem <> 0 Then
CopyMemory ByVal lpMem, bImageData(0), lngByteCount
Call GlobalUnlock(hMem)
If CreateStreamOnHGlobal(hMem, 1, IStream) = 0 Then
If CLSIDFromString(StrPtr( "{7BF80980-BF32-101A-8BBB-00AA00300CAB} "), IID_IPicture(0)) = 0 Then
Call OleLoadPicture(ByVal ObjPtr(IStream), lngByteCount, 0, IID_IPicture(0), GetPictureFromByteStream)
End If
End If
End If
End If
GlobalFree hMem
Exit Function
Err_Init:
MsgBox Err.Number & " - " & Err.Description
End Function
把图像数据存入字节数组,按如下调用即可:
set picture1.picture=GetPictureFromByteStream(bytData())