#include once "window9.bi"

Using GDIPLUS

Dim 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"
EndIf

Function Catch_ImageA(ByVal array As UByte ptr) As Any Ptr Export
	Dim As PVOID pImage,mem,pMem
	Dim SizeRes As Uinteger
	Dim pStream As IStream Ptr
	Dim bmp As HBITMAP
	Dim As Byte ptr bTempSignalBmp = array
	Dim As Byte bTempSignalBmpFlag
	
	SizeRes=UBoundIncBin(array)
	If *bTempSignalBmp = 66 and *(bTempSignalBmp+1)=77 Then
		bTempSignalBmpFlag = 1
	EndIf
	
	If bTempSignalBmpFlag=1 Then
		Dim Bitmapinfo as BITMAPINFO Ptr
		dim BitmapFileHeader as BITMAPFILEHEADER ptr = cast( any ptr, array)
		Dim As HDC hdc=GetDC(0)
		Bitmapinfo = cast(BITMAPINFO ptr, BitmapFileHeader + 1)
		bmp = CreateDIBitmap(hdc, _
		@Bitmapinfo->bmiHeader, _
		CBM_INIT, _
		Cast(byte ptr, BitmapFileHeader) + BitmapFileHeader->bfOffBits, _
		Bitmapinfo, _
		DIB_RGB_COLORS )
		GdipCreateBitmapFromHBITMAP(bmp,0,@pImage)
		DeleteDC(hdc)
		DeleteObject(bmp)
	Else
		mem=GlobalAlloc(GMEM_MOVEABLE,SizeRes+1)
		If mem Then
			pMem = GlobalLock(mem)
			If pMem Then
				CopyMemory(pMem,array,SizeRes+1)
				CreateStreamOnHGlobal(mem,TRUE,@pStream)
				GlobalUnlock(mem)
			EndIf
			If pStream=0 Then
				GlobalFree(mem)
				Return 0
			EndIf
		EndIf
		GdipLoadImageFromStream(pStream, @pImage)
		GlobalFree(mem)
		'GlobalFree(pMem)
	EndIf
	Return pImage
End Function
