#Include "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"
EndIf
Sub RotateAndScaleImageA(_
	Byval GpbitmapSource As Any ptr,_
	ByRef hbmpDest As HBITMAP,_
	ByVal X As Integer,_
	ByVal Y As Integer,_
	ByVal Xr As Integer,_
	ByVal Yr As Integer,_
	ByVal angle As Single,_
	ByVal Xscale As Single=0,_
	ByVal Yscale As Single=0,_
	ByVal Color_ As integer=0,_
	ByVal BGbitmap As Any Ptr=0_
	) Export
	If GpbitmapSource=0 Or hbmpDest=0 Then
		PRINT "GpbitmapSource or hbmpDest FAIL"
		Exit Sub
	EndIf

	Dim As PVOID GPIMAGE_01
	Dim As UInteger Wsource
	Dim As UInteger Hsource
	Dim size As BITMAP
	GdipGetImageWidth(Cast(GpIMAGE Ptr,GpbitmapSource),@Wsource)
	GdipGetImageHeight(Cast(GpIMAGE Ptr,GpbitmapSource),@Hsource)
	GetObject(hbmpDest,sizeof(BITMAP),@size)
	Dim As Integer Wdest=size.bmWidth
	Dim As Integer Hdest=size.bmHeight

	Dim As Pvoid gp
	GdipCreateBitmapFromHBITMAP(hbmpDest, NULL,@gp)
	GdipGetImageGraphicsContext(gp,@GPIMAGE_01)
	If BGbitmap=0 Then
		Dim As PVOID brush
		GdipCreateSolidFill(Color_,@brush)
		GdipFillRectangle(GPIMAGE_01,brush,0,0,Wdest,Hdest)
		GdipDeleteBrush(brush)
	Else
		GdipDrawImageRect(GPIMAGE_01,BGbitmap , 0, 0,Wdest,Hdest)
	EndIf
	If Xscale<>0 And Yscale<>0 Then
		GdipScaleWorldTransform(GPIMAGE_01,Xscale,Yscale,MatrixOrderAppend)
	EndIf
	GdipRotateWorldTransform(GPIMAGE_01, angle, 1)
	GdipTranslateWorldTransform(GPIMAGE_01, X, Y, 1)
	GdipDrawImageRectI(GPIMAGE_01, Cast(GpIMAGE Ptr,GpbitmapSource), Xr, Yr, -Wsource, -Hsource)
	GdipResetWorldTransform(GPIMAGE_01)
	deleteobject(hbmpDest)
	GdipCreateHBITMAPFromBitmap(gp,@hbmpDest,0)
	GdipDisposeImage(gp)
	GdipDeleteGraphics(GPIMAGE_01)
End Sub
