Rotate a bitmap by an arbitrary angle
Posted: 19 Mar 2017 17:16
I am just starting to explore GDI+ and the depth of my understanding is very shallow. By way of exploration, I decided to tackle rotating a resource bitmap by an arbitrary angle and still have not fully absorbed how pointers, handles, bitmapIDs, and actual images are managed by VP and GDI.
My little test code below is intended to load a bitmap, draw it, rotate it 90 degrees, draw that rotated bitmap, then rotate a copy of the original bitmap 33.3 degrees and draw that bitmap. I am stumped on how to handle the last part of the test--the first two parts are simply to verify that I can draw something.
The predicate to rotate a bitmap is below:
My little test code below is intended to load a bitmap, draw it, rotate it 90 degrees, draw that rotated bitmap, then rotate a copy of the original bitmap 33.3 degrees and draw that bitmap. I am stumped on how to handle the last part of the test--the first two parts are simply to verify that I can draw something.
Code: Select all
predicates
onPaint : window::paintResponder.
clauses
onPaint( _Source, _Rectangle, GDI):-
Pnt1 = pnt(100,100),
Pnt2 = pnt(150,100),
Pnt3 = pnt(200,100),
Angle = 33.3, % angle to rotate bitmap below
% Bitmap name in resources = "BoatStbdTack"
PicB = vpi::pictGetFromRes( resourceIdentifiers::idb_boatstbdtack ), % returns a PICTURE (a POINTER to the the bitmap)
GDI:pictDraw( PicB, Pnt1, rop_SrcAnd ), % operates on the POINTER to the picture
PtrB90 = vpi::pictRotate(PicB,pictRotate_90deg ), % counter-clockwise rotation, only in 90 degree steps
GDI:pictDraw( PtrB90, Pnt2, rop_SrcAnd ),
/*
This is where I am stumped.
How can I create BMP_ToROTATE (an actual bitmap) to rotate using rotateImage/2 below ?
Perhaps rotateImage/2 should be modified to work with pictures (pointers) instead of actual bitmaps ...
*/
NewRotBMP = rotateImage( BMP_ToROTATE, Angle ),
GDI:pictDraw( NewRotBMP ,Pnt3,rop_SrcAnd),
succeed.
Code: Select all
class predicates
rotateImage : ( bitmap, real AngleToRotate ) -> bitmap RotatedBMP.
clauses
rotateImage( BMP, Angle ) = RotatedBMP :-
G = graphics::createFromImage( BMP ), % create new graphics canvas
% move rotation point to center of image
G:translateTransform( BMP:width/2, BMP:height /2 ),
% rotate the graphics
G:rotateTransform( Angle ),
% move the image back
G:translateTransform( - BMP:width/2, - BMP:height/2 ),
% create bitmap from rotated graphics
RotatedBMP = bitmap::createFromGraphics( BMP:width, BMP:height, G).