none
将图片存储到数据库的ole字段中,如何直接将图片显示在picturebox上? RRS feed

  • 常规讨论

  • 简单一点用控件绑定,复杂一点用API:


    Declare Function GlobalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal dwBytes As Long) As Long
    Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
    Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
    Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
    Declare Function CreateStreamOnHGlobal Lib "ole32" (ByVal hGlobal As Long, ByVal fDeleteOnRelease As CBoolean, ppstm As Any) As Long
    Declare Function OleLoadPicture Lib "olepro32" (pStream As Any, ByVal lSize As Long, ByVal fRunmode As CBoolean, riid As GUID, ppvObj As Any) As Long
    Declare Function CLSIDFromString Lib "ole32" (ByVal lpsz As Any, pclsid As GUID) As Long
    Public Const S_OK = 0
    Public Const sIID_IPicture = "{7BF80980-BF32-101A-8BBB-00AA00300CAB}"
    Public Const GMEM_MOVEABLE = &H2000
    Enum CBoolean ' enum members are Long data types
    CFalse = 0
    CTrue = 1
    End Enum
    Public 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


    Function PictureFromBits(abPic() As Byte) As IPicture
    Dim nLow As Long
    Dim cbMem As Long
    Dim hMem As Long
    Dim lpMem As Long
    Dim IID_IPicture As GUID
    Dim IsTm As stdole.IUnknown ' IStream
    Dim IPic As IPicture
    On Error GoTo Out
    nLow = LBound(abPic)
    On Error GoTo 0
    cbMem = (UBound(abPic) - nLow) & 1
    hMem = GlobalAlloc(GMEM_MOVEABLE, cbMem)
    If hMem Then
    lpMem = GlobalLock(hMem)
    If lpMem Then
    MoveMemory ByVal lpMem, abPic(nLow), cbMem
    GlobalUnlock hMem
    If (CreateStreamOnHGlobal(hMem, CTrue, IsTm) = S_OK) Then
    If (CLSIDFromString(StrPtr(sIID_IPicture), IID_IPicture) = S_OK) Then
    OleLoadPicture ByVal ObjPtr(IsTm), cbMem, CFalse, IID_IPicture, PictureFromBits
    End If
    End If
    End If
    End If
    Out:
    End Function
    2009年5月27日 8:17