'利用IPersistStream接口和IStream接口实现 '可以从http://www.mvps.org/emorcillo/vb6/tlb/tl_ole.zip下载文件,下载后解压,并注册、引用olelib.tlbOption ExplicitConst GMEM_MOVEABLE = &H2 Const GMEM_ZEROINIT = &H40 Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT) Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem 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 LongConst PictureID = &H746C&Private Type PictureHeader Magic As Long Size As Long End Type Public Sub Picture2Array(ByVal oObj As StdPicture, aBytes() As Byte) Dim oIPS As IPersistStream Dim oStream As IStream Dim hGlobal As Long Dim lPtr As Long Dim lSize As Long Dim Hdr As PictureHeader Set oIPS = oObj Set oStream = CreateStreamOnHGlobal(0, True) oIPS.Save oStream, True hGlobal = GetHGlobalFromStream(oStream) lSize = GlobalSize(hGlobal) lPtr = GlobalLock(hGlobal) If lPtr Then lSize = lSize - Len(Hdr) ReDim aBytes(0 To lSize - 1) MoveMemory aBytes(0), ByVal lPtr + Len(Hdr), lSize End If GlobalUnlock hGlobal Set oStream = NothingEnd Sub Public Function Array2Picture(aBytes() As Byte) As StdPicture Dim oIPS As IPersistStream Dim oStream As IStream Dim hGlobal As Long Dim lPtr As Long Dim lSize As Long Dim Hdr As PictureHeader Set Array2Picture = New StdPicture Set oIPS = Array2Picture lSize = UBound(aBytes) - LBound(aBytes) + 1 hGlobal = GlobalAlloc(GHND, lSize + Len(Hdr)) If hGlobal Then lPtr = GlobalLock(hGlobal) Hdr.Magic = PictureID Hdr.Size = lSize MoveMemory ByVal lPtr, Hdr, Len(Hdr) MoveMemory ByVal lPtr + Len(Hdr), aBytes(0), lSize GlobalUnlock hGlobal Set oStream = CreateStreamOnHGlobal(hGlobal, True) oIPS.Load oStream Set oStream = Nothing End If End Function Private Sub Command1_Click() Dim buff() As Byte Picture2Array Picture1.Picture, buff '测试 Set Picture2.Picture = Array2Picture(buff) End Sub
我用过getDIBits、setDIBits ,感觉还行。
后来改成array2d,copymemory,速度不错,似乎此方案已达到vb的极限。
楼主可以去查查相关资料。
里面有byte 直接读取
http://blog.csdn.net/zlt982001/archive/2005/09/27/490378.aspx
'可以从http://www.mvps.org/emorcillo/vb6/tlb/tl_ole.zip下载文件,下载后解压,并注册、引用olelib.tlbOption ExplicitConst GMEM_MOVEABLE = &H2
Const GMEM_ZEROINIT = &H40
Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem 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 LongConst PictureID = &H746C&Private Type PictureHeader
Magic As Long
Size As Long
End Type
Public Sub Picture2Array(ByVal oObj As StdPicture, aBytes() As Byte)
Dim oIPS As IPersistStream
Dim oStream As IStream
Dim hGlobal As Long
Dim lPtr As Long
Dim lSize As Long
Dim Hdr As PictureHeader
Set oIPS = oObj
Set oStream = CreateStreamOnHGlobal(0, True)
oIPS.Save oStream, True
hGlobal = GetHGlobalFromStream(oStream)
lSize = GlobalSize(hGlobal)
lPtr = GlobalLock(hGlobal)
If lPtr Then
lSize = lSize - Len(Hdr)
ReDim aBytes(0 To lSize - 1)
MoveMemory aBytes(0), ByVal lPtr + Len(Hdr), lSize
End If
GlobalUnlock hGlobal
Set oStream = NothingEnd Sub
Public Function Array2Picture(aBytes() As Byte) As StdPicture
Dim oIPS As IPersistStream
Dim oStream As IStream
Dim hGlobal As Long
Dim lPtr As Long
Dim lSize As Long
Dim Hdr As PictureHeader
Set Array2Picture = New StdPicture
Set oIPS = Array2Picture
lSize = UBound(aBytes) - LBound(aBytes) + 1
hGlobal = GlobalAlloc(GHND, lSize + Len(Hdr))
If hGlobal Then
lPtr = GlobalLock(hGlobal)
Hdr.Magic = PictureID
Hdr.Size = lSize
MoveMemory ByVal lPtr, Hdr, Len(Hdr)
MoveMemory ByVal lPtr + Len(Hdr), aBytes(0), lSize
GlobalUnlock hGlobal
Set oStream = CreateStreamOnHGlobal(hGlobal, True)
oIPS.Load oStream
Set oStream = Nothing
End If
End Function
Private Sub Command1_Click()
Dim buff() As Byte
Picture2Array Picture1.Picture, buff
'测试
Set Picture2.Picture = Array2Picture(buff)
End Sub
'可以从http://www.mvps.org/emorcillo/vb6/tlb/tl_ole.zip下载文件,下载后解压,并注册、引用olelib.tlb=============================
http 404 被删了?
这是现在的地址
Dim oIPS As IPersistStream
Dim oStream As IStream
Dim hGlobal As Long
说没有定义呢??