#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 RotateAndScaleImage(_
	Byval hbmpSource As HBITMAP,_
	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 HBITMAP=0_
	) Export
	If hbmpSource=0 Or hbmpDest=0 Then
		Print "hbmpSource or hbmpDest fail"
		Exit sub
	EndIf
	Dim As GpBitmap Ptr GPIMAGE_01,GPIMAGE_02,GPIMAGE_BG
	Dim As PVOID Graphics
	Dim size As BITMAP
	GetObject(hbmpSource,sizeof(BITMAP),@size)
	Dim As Integer Wsource=size.bmWidth
	Dim As Integer Hsource=size.bmHeight
	GetObject(hbmpDest,sizeof(BITMAP),@size)
	Dim As Integer Wdest=size.bmWidth
	Dim As Integer Hdest=size.bmHeight
	GdipCreateBitmapFromHBITMAP(hbmpSource,0,@GPIMAGE_01)
	GdipCreateBitmapFromHBITMAP(hbmpDest,0, @GPIMAGE_02)
	GdipGetImageGraphicsContext(Cast(GpImage Ptr,GPIMAGE_02),@Graphics)
	If BGbitmap=0 Then
		Dim As PVOID brush
		GdipCreateSolidFill(Color_,@brush)
		GdipFillRectangle(Graphics,brush,0,0,Wdest,Hdest)
		GdipDeleteBrush(brush)
	Else
		GdipCreateBitmapFromHBITMAP(BGbitmap,0,@GPIMAGE_BG)
		GdipDrawImageRect(Graphics,Cast(GpImage Ptr,GPIMAGE_BG) , 0, 0,Wdest,Hdest)
	EndIf
	If Xscale<>0 And Yscale<>0 Then
		GdipScaleWorldTransform(Graphics,Xscale,Yscale,MatrixOrderAppend)
	EndIf
	GdipRotateWorldTransform(Graphics, angle, 1)
	GdipTranslateWorldTransform(Graphics, X, Y, 1)
	GdipDrawImageRectI(Graphics, Cast(GpImage Ptr,GPIMAGE_01), Xr, Yr, -Wsource, -Hsource)
	GdipResetWorldTransform(Graphics)
	DeleteObject(hbmpDest)
	GdipCreateHBITMAPFromBitmap(GPIMAGE_02,@hbmpDest,0)
	GdipDisposeImage (Cast(GpImage Ptr,GPIMAGE_01))
	GdipDisposeImage (Cast(GpImage Ptr,GPIMAGE_02))
	If BGbitmap<>0 Then
		GdipDisposeImage (Cast(GpImage Ptr,GPIMAGE_BG))
	EndIf
	GdipDeleteGraphics(Graphics)
End Sub

