#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 SAVE_imageA(ByVal GpBitmap As Any Ptr, ByRef sFileName As USTRING) as Integer Export
	Dim As String clssid
	If LCase(Right(sFileName,3))="bmp" Then
		clssid="{557CF400-1A04-11D3-9A73-0000F81EF32E}"
	ElseIf LCase(Right(sFileName,4))="jpeg" Or LCase(Right(sFileName,3))="jpg" Then
		clssid="{557CF401-1A04-11D3-9A73-0000F81EF32E}"
	ElseIf LCase(Right(sFileName,3))="gif" Then
		clssid="{557CF402-1A04-11D3-9A73-0000F81EF32E}"
	ElseIf LCase(Right(sFileName,4))="tiff" Then
		clssid="{557CF405-1A04-11D3-9A73-0000F81EF32E}"
	ElseIf LCase(Right(sFileName,3))="png" Then
		clssid="{557CF406-1A04-11D3-9A73-0000F81EF32E}"
	EndIf
	DIM CLSID_01 AS CLSID
	CLSIDFROMSTRING WSTR(clssid), @CLSID_01	
	#ifdef UNICODE
		IF (GDIPSAVEIMAGETOFILE(GpBitmap, *sFileName, @CLSID_01 , 0) <> 0) Then
			PRINT "FAIL"
		EndIf		
	#else	
		Var blen = Len(sFileName)*2+2
		Dim As WString Ptr wbuf = Allocate( blen )
		if wbuf = 0 then return 0
		MultiByteToWideChar(CP_ACP, 0, StrPtr(sFileName), -1, wbuf, blen)
		IF (GDIPSAVEIMAGETOFILE(GpBitmap, *wbuf, @CLSID_01 , 0) <> 0) Then
			PRINT "FAIL"
		EndIf
		DeAllocate(wbuf)		
	#EndIf
	Return 1
End Function
