#include once "window9.bi"

Using GDIPLUS

Dim shared ULONG_PTR_01 AS ULONG_PTR
DIM GDIPLUSSTARTUPINPUT_01 AS GDIPLUSSTARTUPINPUT
GDIPLUSSTARTUPINPUT_01.GdiplusVersion = 1
IF (GDIPLUSSTARTUP(@ULONG_PTR_01, @GDIPLUSSTARTUPINPUT_01, NULL) <> 0) THEN
	PRINT "FAIL"
End If

Function LoadImageFromResource(byval lpResName As any ptr, byval color_ As Integer) As HBITMAP Export
	Dim As HINSTANCE hInst
	Dim As PVOID pImage
	Dim As HRSRC hrsrc
	Dim As HGLOBAL hResGlob
	Dim As DWORD SizeRes
	Dim As LPBYTE pRes
	Dim As IStream ptr pStream
	Dim bmp As HBITMAP
	
	hInst=GetModuleHandle(0)
	hrsrc = FindResource(hInst,lpResName,RT_RCDATA)
	If hrsrc=0 Then Return 0
	hResGlob = LoadResource(hInst, hrsrc)
	SizeRes = SizeofResource(hInst, hrsrc)
	pRes = LockResource(hResGlob)
	Dim As HGLOBAL hgMem = GlobalAlloc(GMEM_MOVEABLE,SizeRes)
	If hgMem=0 Then Return 0
	Dim As LPBYTE pMem = GlobalLock(hgMem)
	CopyMemory(pMem, pRes, SizeRes)
	GlobalUnlock(hgMem)
	FreeResource(hResGlob)
	If (CreateStreamOnHGlobal(hgMem,TRUE,@pStream))<>0 Then
		GlobalFree(hgMem)
		Return 0
	EndIf
	GdipLoadImageFromStream(pStream,@pImage)
	If LOBYTE(LOWORD(GetVersion)) <6 Then
		DIM AS PVOID PTR GPIMAGE_02,GpGraphics
		Dim As long w,h
		GdipGetImageWidth(pImage,@w)
		GdipGetImageHeight(pImage,@h)
		GdipCreateBitmapFromScan0(w, h, 0, PixelFormat32bppARGB, NULL, Cast(Any Ptr,@GPIMAGE_02))
		GdipGetImageGraphicsContext(Cast(Any Ptr,GPIMAGE_02),Cast(Any Ptr,@GpGraphics))
		GdipGraphicsClear(Cast(Any Ptr,GpGraphics),color_ or &hff000000)
		GdipDrawImageRectI(Cast(Any Ptr,GpGraphics),pImage,0,0,w,h)
		GdipCreateHBITMAPFromBitmap(Cast(Any Ptr,GPIMAGE_02),@bmp,color_ or &hff000000)
		If GPIMAGE_02 <> 0 Then GdipDisposeImage(Cast(Any Ptr,GPIMAGE_02))
		If GpGraphics <> 0 Then GdipDeleteGraphics(Cast(Any Ptr,GpGraphics))
	Else
		GdipCreateHBITMAPFromBitmap(pImage,@bmp,color_ or &hff000000)
	EndIf
	'GlobalFree(pMem)
	GlobalFree(hgMem)
	GdipDisposeImage(pImage)
	return bmp
End Function
