As a continuation to the OleLoadPicturePath (http://www.masm32.com/board/index.php?topic=10850.0) thread, I created a static library to display images based on the usage of OLE functions.
LoadImageFromFile PROC pImageFileName:DWORD
This function loads a BMP, JPG, GIF or WMF image from disc and returns a handle to the image.
pImageFileName is a pointer to the FULL path name of the image file to be displayed
LoadImageFromMem PROC pImageAddr:DWORD,ImageLen:DWORD
This function returns the handle of an image stored in memory. Valid image formats are BMP, JPG, GIF and WMF
pImageAddr is a pointer to the location of the image in memory.
ImageLen is the size of the image.
In case of error, both of the functions will return NULL.
LoadImageFromFile PROC pImageFileName:DWORD
LOCAL hHeap:DWORD
LOCAL pMem:DWORD
LOCAL hOLEimg:DWORD
LOCAL hBitmap:DWORD
LOCAL pPicture:DWORD
mov hBitmap,0
mov pPicture,0
invoke GetProcessHeap
test eax,eax
jz _exit
mov hHeap,eax
invoke HeapAlloc,eax,HEAP_ZERO_MEMORY,2*MAX_PATH
test eax,eax
jz _exit
mov pMem,eax
invoke MultiByteToWideChar,CP_ACP,NULL,pImageFileName,-1,eax,2*MAX_PATH
test eax,eax
jz FreeMem
invoke OleLoadPicturePath,pMem,0,0,0,ADDR IID_IPicture,ADDR pPicture
test eax,eax
jnz FreeMem
lea eax,hOLEimg
push eax
mov eax,pPicture
push eax
mov eax,DWORD PTR [eax]
call IPicture.get_Handle[eax]
test eax,eax
jnz ReleasePict
invoke CopyImage,hOLEimg,IMAGE_BITMAP,0,0,LR_COPYRETURNORG
test eax,eax
jz ReleasePict
mov hBitmap,eax
ReleasePict:
mov eax,pPicture
push eax
mov eax,DWORD PTR [eax]
call IPicture.Release[eax]
FreeMem:
invoke HeapFree,hHeap,0,pMem
_exit:
mov eax,hBitmap
ret
LoadImageFromFile ENDP
LoadImageFromMem PROC USES esi edi pImageAddr:DWORD,ImageLen:DWORD
LOCAL hOLEimg:DWORD
LOCAL hBitmap:DWORD
LOCAL pPicture:DWORD
LOCAL pStream:DWORD
LOCAL hGlobal:DWORD
mov hBitmap,0
mov pStream,0
mov pPicture,0
invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_NODISCARD,ImageLen
mov hGlobal,eax
invoke GlobalLock,eax
cld
mov esi,pImageAddr
mov edi,eax
mov ecx,ImageLen
shr ecx,2
rep movsd
mov ecx,ImageLen
and ecx,3
rep movsb
invoke CreateStreamOnHGlobal,hGlobal,TRUE,ADDR pStream
test eax,eax
jnz _exit
invoke OleLoadPicture,pStream,0,FALSE,ADDR IID_IPicture,ADDR pPicture
test eax,eax
jnz ReleaseStream
lea eax,hOLEimg
push eax
mov eax,pPicture
push eax
mov eax,DWORD PTR [eax]
call IPicture.get_Handle[eax]
test eax,eax
jnz ReleasePict
invoke CopyImage,hOLEimg,IMAGE_BITMAP,0,0,LR_COPYRETURNORG
test eax,eax
jz ReleasePict
mov hBitmap,eax
ReleasePict:
mov eax,pPicture
push eax
mov eax,DWORD PTR [eax]
call IPicture.Release[eax]
ReleaseStream:
mov eax,pStream
push eax
mov eax,DWORD PTR [eax]
call IStream.IUnknown.Release[eax]
_exit:
mov eax,hBitmap
ret
LoadImageFromMem ENDP
[attachment deleted by admin]
Thank you,very nice as always.
Paul
- Precaution against memory allocation failure : LoadImageFromMem checks if GlobalAlloc fails or not.
invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_NODISCARD,ImageLen
test eax,eax
jz _exit
mov hGlobal,eax
invoke GlobalLock,eax
test eax,eax
jz _exit
.
.
.
invoke CreateStreamOnHGlobal,hGlobal,TRUE,ADDR pStream
test eax,eax
jz @f
invoke GlobalUnlock,hGlobal
invoke GlobalFree,hGlobal
jmp _exit
[attachment deleted by admin]