#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_Image(byval array As Ubyte ptr, byval color_ As COLORREF=&hf0f0f0) As HBITMAP 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 )
		DeleteDC(hdc)
	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)
		If pImage Then
			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_)
				GdipDrawImageRectI(Cast(Any Ptr,GpGraphics),pImage,0,0,w,h)
				GdipCreateHBITMAPFromBitmap(Cast(Any Ptr,GPIMAGE_02),@bmp,color_)
				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_)
			EndIf
		EndIf
		GlobalFree(mem)
		'GlobalFree(pMem)
		GdipDisposeImage(pImage)
	EndIf
	Return bmp
End Function
