二进制图片的显示问题

hs912 2007-12-31 09:52:13
我把图片读成了二进制数组后,现在想在picturebox里显示这个图片
当然不能把图片保存为外存文件再loadpicture,那样效率实在太低
现在我想直接利用这个二进制数组,把图片显示到picturebox中,应该怎么办,高手指教!
...全文
169 点赞 收藏 10
写回复
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sandrer 2008-01-01
如果你坚持要用数组的话,那么~~~~~~~~~~~~~~~~~~~~~~


Option Explicit

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" (pStream As Any, ByVal lSize As Long, ByVal fRunmode As Long, riid As Any, ppvObj As Any) As Long
Private Declare Function CLSIDFromString Lib "ole32" (ByVal lpsz As Any, pclsid As Any) 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 Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Const GMEM_ZEROINIT = &H40

Private 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

Private Sub Command1_Click()
Dim bytData() As Byte
Dim f As String
Dim Fn As Integer
f = App.Path & "\p.jpg"
If Dir(f) = "" Then
MsgBox "File not found"
Exit Sub
End If
Fn = FreeFile
Open f For Binary As #Fn
ReDim bytData(LOF(1) - 1)
Get #Fn, , bytData
Close #Fn
Set Picture1.Picture = GetPictureFromByteStream(bytData())
End Sub
回复
Sandrer 2008-01-01
我还想问一下,既然你要追求效率的话,一幅图片有多大?
你的二进制数组要消耗多少内存????
回复
Sandrer 2008-01-01
想问一下,你的图片格式是什么????
如果是位图的话还比较好处理,Gif或者jpg的话就..............
回复
hs912 2008-01-01
Sandrer 效率比loadpicture还低,怎么办?
回复
hs912 2008-01-01
居然用到这么多api不知道效率如何,我去测试一下
回复
hs912 2008-01-01
谢谢Sandrer
回复
Sandrer 2008-01-01
你看看我在5楼问你的问题嘛~
保存在内存中的数据不一定就读写得快的~
本来用数组的情况下效率就不是很高,而且你的数组估计上限也很大
回复
hs912 2007-12-31
多线程的。这个不重要,总之为了提高效率,我不能容忍LoadPicture的低效率,现在数据明明在内存中了,怎么都不能让我去从外存再读一次了!
回复
Sandrer 2007-12-31
我想知道的是,在LoadPicture没有返回的情况下,如何执行LoadPicture后面的代码?
如果你后面的处理代码在执行,那么表示LoadPicture已经加载好图片了~
多线程?
回复
hs912 2007-12-31
注:就是因为使用loadpicture效率低下,经常导致我处理图片都开始了,结果loadpicture还没加载完,这样,后面处理功能经常出错。
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7453

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告