[代码见内,使用资源来载入JPG格式图片问题]使用LoadResData后Byte()如何给一个IPictureDisp对象赋值?

Gutta 2006-01-24 08:21:56
Private Sub Form_Load()
Dim bs() As Byte
'自定义的101号资源就是外部的一个JPG资源
bs = LoadResData(101, "CUSTOM")

Me.Picture =...
End Sub

想把这个JPG资源做为桌面背景,如何把一个Byte()数据放到一个IPicutreDisp中去呢???
...全文
131 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Gutta 2006-01-24
其实不光是资源,在载入图片的数据库字段的时候也会出现这个问题。

Byte()数组转化为图片的问题,老实说,这个方法还是很有必要的。。。

收藏!
回复
Gutta 2006-01-24
谢谢老暴的解答

算了,简单一点还是用文件吧。。。
郁闷
回复
rainstormmaster 2006-01-24
当然,你也可以先保存为临时文件,再loadpicture载入图片
回复
rainstormmaster 2006-01-24
'利用IPersistStream接口和IStream接口实现
'可以从http://www.mvps.org/emorcillo/download/vb6/tl_ole.zip下载文件,下载后解压,并注册、引用olelib.tlb

Option Explicit
Const 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 Long

Const 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 = Nothing

End 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 bs() As Byte
'自定义的101号资源就是外部的一个JPG资源
bs = LoadResData(101, "CUSTOM")
Set Me.Picture = Array2Picture(bs)
End Sub
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7453

社区成员

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