With the API function OleLoadPicture, it's possible to get the handle of images stored in an executable :
GUI "Form1", PIXELS
MACRO IMAGE_SIZE = 10625
GLOBAL Form1 AS HWND
DIM hBitmap AS HBITMAP
EXTERN ImageLabel AS void PTR
SUB FORMLOAD()
Form1 = BCX_FORM("LoadImageFromMem Demo", 0, 0, 380, 338)
CENTER(Form1)
SHOW(Form1)
END SUB
BEGIN EVENTS
SELECT CASE CBMSG
CASE WM_CREATE
hBitmap = LoadImageFromMem(&ImageLabel, IMAGE_SIZE)
EXIT FUNCTION
CASE WM_PAINT
DIM RAW ps AS PAINTSTRUCT
DIM RAW hdc AS HDC
DIM RAW hMemDC AS HDC
DIM RAW bm AS BITMAP
DIM RAW hOldBitmap AS HBITMAP
hdc = BeginPaint(hWnd, &ps)
hMemDC = CreateCompatibleDC(hdc)
hOldBitmap = SelectObject(hMemDC, hBitmap)
GetObject(hBitmap, SIZEOF(BITMAP), &bm)
BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, _
hMemDC, 0, 0, SRCCOPY)
SelectObject(hMemDC, hOldBitmap)
DeleteDC(hMemDC)
EndPaint(hWnd, &ps)
EXIT FUNCTION
END SELECT
END EVENTS
FUNCTION LoadImageFromMem(ImageFile AS void PTR, ImageLen AS INTEGER) AS HBITMAP
DIM AS LPSTREAM pStream
DIM AS HGLOBAL hGlobal
DIM AS HRESULT hr
DIM AS IPicture PTR IPict
DIM AS HBITMAP hBmp
DIM AS HBITMAP hOleBmp
DIM AS LPVOID pMem
hGlobal = GlobalAlloc(GMEM_MOVEABLE, ImageLen)
pMem = GlobalLock(hGlobal)
memcpy(pMem, ImageFile, ImageLen)
CreateStreamOnHGlobal(pMem, TRUE, &pStream)
OleLoadPicture(pStream, 0, FALSE, &IID_IPicture, (LPVOID PTR)&IPict)
hr = IPict->lpVtbl->get_Handle(IPict, (OLE_HANDLE PTR)&hOleBmp)
hBmp = CopyImage(hOleBmp, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG)
hr = IPict->lpVtbl->Release(IPict)
hr = pStream->lpVtbl->Release(pStream)
FUNCTION = hBmp
END FUNCTION
The trick to embed the image is to convert it to a linkable MS COFF object module, ImgToMSCOFF.bat :
\PellesC\bin\poasm.exe /A%1 Image.asm
Examples, 32-bit and 64-bit MS COFF object files :
ImgToMSCOFF.bat IA32 Image.jpg
ImgToMSCOFF.bat AMD64 Image.jpg
While building the project with BED, you need to specift the name of the object file in the field Binary files to link :
Image.obj
Image.asm :
; @ModeBits : The current parser mode. An integer constant.
; Either 32 (for IA32) or 64 (for AMD64).
IF @ModeBits EQ 32
.386
.model flat,stdcall
option casemap:none
ENDIF
PUBLIC ImageLabel
.data
ImageLabel:
INCBIN image.jpg
The powerful POASM statement INCBIN includes any binary file within the source assembly code.
You need to set the size of the image file here in the source code above :
MACRO IMAGE_SIZE = 10625