|
|
这或许不是最好的方法,但绝对可行,注意以下是我的图片打包程序中截取片断稍加修改的,领会精神就可以,复制粘贴不一定能运行。
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 Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
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 SelectObject Lib "gdi32" (ByVal HDC As Long, ByVal hObject As Long) As Long
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal HDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal HDC As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Dim memDC As Long
Dim lpMem As Long
Dim IID_IPicture(15)
Dim istm As stdole.IUnknown
Dim myIPicture As IPicture
Dim myStdPicture As StdPicture
Dim TempDC As Long, hBmp As Long, w As Long, h As Long, bmpInfo As Bitmap
Dim b() As Byte
'1. 把图片读入数组b()中,假设这步骤已经完成。
'2. 从数组b()中建立图片对象,并存放在一个内存DC中
memDC = GlobalAlloc(&H2, Ubound(b))
If memDC <> 0 Then
lpMem = GlobalLock(memDC)
If lpMem <> 0 Then
MoveMemory ByVal lpMem, b(0), Ubound(b)
Call GlobalUnlock(memDC)
If CreateStreamOnHGlobal(memDC, 1, istm) = 0 Then
If CLSIDFromString(StrPtr("{7BF80980-BF32-101A-8BBB-00AA00300CAB}"), IID_IPicture(0)) = 0 Then
Call OleLoadPicture(ByVal ObjPtr(istm), ByteCount, 0, IID_IPicture(0), myIPicture)
End If
End If
End If
End If
Set myStdPicture = myIPicture
GetObject myStdPicture.Handle, Len(bmpInfo), bmpInfo
w = bmpInfo.bmWidth
h = bmpInfo.bmHeight
TempDC = CreateCompatibleDC(0)
hBmp = SelectObject(TempDC, myStdPicture.Handle)
'TempDC 就是存有图片的内存DC的指针
'3. 如何显示图片: BitBlt picture1.hDC, 0, 0, w, h, TempDC, 0, 0, SRCCOPY
'4. 其实它最大的用处就是可以实现图片资源打包,并在内存中读取,还原,不用生成临时图片文件。
|
|