比较特殊的问题。有关GDI载入图片
问题的背景是这样的。。
我用open for binary 打开了 a.jpg 用get方法把他保存在了字节数组 data() as byte中
然后有没有办法直接通过 字节数组 来显示a.jpg?不通过loadfromfile的形式
[解决办法]
小case,U1KProject中就用到了,而且还在这基础上实现的窗体背景图。代码摘录如下:
调用
Public BackPic As StdPicture '背景图
Sub Main()
Set BackPic = PictureFromBits(LoadResData(105, "CUSTOM")) 'pic() 是图片的二进制数据。
支持的标准模块:
Attribute VB_Name = "mdlJpgBits"
'http://bbs.csdn.net/topics/215563
' 高分求解图片处理问题! [问题点数:300分,结帖人huanggx]
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 GUID, _
ppvObj As Any) As Long
Public Type GUID
dwData1 As Long
wData2 As Integer
wData3 As Integer
abData4(7) As Byte
End Type
Private Declare Function CLSIDFromString Lib "ole32" (ByVal lpsz As Any, pclsid As GUID) As Long
Private Const sIID_IPicture = "{7BF80980-BF32-101A-8BBB-00AA00300CAB}"
Private Const GMEM_MOVEABLE = &H2
Private Const S_OK = 0
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 MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
'_______________________________________________________________
Public 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
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
Call GlobalUnlock(hMem)
If (CreateStreamOnHGlobal(hMem, 1, istm) = S_OK) Then
If (CLSIDFromString(StrPtr(sIID_IPicture), IID_IPicture) = S_OK) Then
Call OleLoadPicture(ByVal ObjPtr(istm), cbMem, 0, IID_IPicture, PictureFromBits)
End If
End If
End If
End If
Out:
End Function
Dim pic as picture
Dim picmem() as byte
' 去读数据到数组
...
' 开始将数组转换成图像
Set pic = PictureFromBits(picmem)
picture1.PaintPicture pic,0,0
...