#Include "windows.bi"
#Include "win/gdiplus.bi"
#Include "crt.bi"

Dim As Guid EncoderSaveFlag = Type(&h292266fc,&hac40,&h47bf,{&h8c, &hfc, &ha8, &h5b, &h89, &ha6, &h55, &hde})

Using GDIPLUS
Dim GDIPLUSSTARTUPINPUT As GDIPLUSSTARTUPINPUT
Dim As ULONG_PTR gdiplusToken
Dim Shared As GPIMAGE Ptr page(1 To 4)

GDIPLUSSTARTUPINPUT.GdiplusVersion = 1
If (GdiplusStartup(@gdiplusToken, @GDIPLUSSTARTUPINPUT, NULL) <> 0) Then
	Print "FAIL"
EndIf

Function GetEncoderClsid(format_ As WString Ptr , ByRef pClsid As CLSID Ptr ) As Integer

	Dim As UINT  num
	Dim As UINT  size

	Dim As ImageCodecInfo Ptr pImageCodecInfo

	GdipGetImageEncodersSize(@num, @size)
	If(size = 0) Then Return -1

	pImageCodecInfo = Allocate(size)
	If pImageCodecInfo = NULL Then Return -1


	GdipGetImageEncoders(num, size, pImageCodecInfo)

	For j As UInteger = 0 To num-1
		If wcscmp(pImageCodecInfo[j].MimeType, format_) = 0  Then
			*pClsid = pImageCodecInfo[j].Clsid
			DeAllocate(pImageCodecInfo)
			Return j
		EndIf
	Next

	DeAllocate(pImageCodecInfo)
	Return -1
End Function


Dim As CLSID encoderClsid
GetEncoderClsid(WStr("image/gif"), @encoderClsid)

For i As Integer = 1 To 4
	If (GdipLoadImageFromFile(WStr(i & ".png"), @page(i)) <> 0) Then
		Print "FAIL"
	EndIf
Next


Dim item As PROPERTYITEM
Dim lDelays(3) As Long = {30,30,30,30} ' время для каждого кадра
item.ID = PropertyTagFrameDelay
item.length = (UBound(lDelays) - LBound(lDelays) + 1) * 4
item.Type = PropertyTagTypeSLONG
item.Value = @lDelays(0)
GdipSetPropertyItem(page(1), @item)

Dim iProperties(1) As Integer = {9} '- 10 циклов 
item.ID = PropertyTagLoopCount
item.length = 2
item.Type = PropertyTagTypeShort
item.Value = @iProperties(0)
GdipSetPropertyItem(page(1), @item)


Dim As EncoderParameters encoderParameters
Dim As ULong parameterValue
encoderParameters.Count = 1
encoderParameters.Parameter(0).Guid = EncoderSaveFlag
encoderParameters.Parameter(0).Type = EncoderParameterValueTypeLong
encoderParameters.Parameter(0).NumberOfValues = 1
encoderParameters.Parameter(0).Value = @parameterValue

parameterValue = EncoderValueMultiFrame
If (GdipSaveImageToFile(page(1), WStr("MultiFrame.gif"), @encoderClsid, @encoderParameters) = 0) Then
	print "Page 1 saved successfully."
EndIf

For i As Integer = 2 To 4
	parameterValue = EncoderValueFrameDimensionTime
	If GdipSaveAddImage(page(1),page(i) , @encoderParameters) = 0 Then
		Print "Page " & i & " saved successfully."
	EndIf
	GdipDisposeImage page(i)
Next

parameterValue = EncoderValueFlush
If GdipSaveAdd(page(1),@encoderParameters) = 0 Then print "File closed successfully."
GdipDisposeImage page(1)
GdiplusShutdown gdiplusToken
sleep