Once upon a time, reading this question, I wondered how to rotate a bitmap by any degree without fiddling around with all the bits myself. Recently, someone else had obvious difficulties with it too.
There are already many questions dealing with rotation at 90° intervals, most notabaly this one, but I want to rotate by a real angle. Preferably with the possibility to adjust the image size due to the rotation, and with setting a custom (transparent) background color for the parts that will be added to image surface. I then suppose the signature of the routine would look something like:
procedure RotateBitmap(Bmp: TBitmap; Angle: Single; AdjustSize: Boolean;
BackColor: TColor);
These answers mention the following candidates for constructing this routine: SetWorldTransform, PlgBlt, GDI+, but I would like to see an (efficient) implementation.
SetWorldTransform
With WinAPI’s SetWorldTransform you can transform the space of device context: rotate, shear, offset, and scale. This is done by setting the members of a transform matrix of type XFORM. Fill its members according the documentation.
PlgBlt
The PlgBlt function performs a bit-block transfer from the specified rectangle in the source device context to the specified parallelogram in the destination device context. Map the corner points of the source image via the
lpPointparameter.Graphics32
Graphics32 is a library especially designed for fast bitmap handling. It requires some experience to grasp its full potential, but the documentation as well as the provided examples should get you started.
A rotation of a
TBitmap32image is done by transforming it by one of the many available transformation classes. TheTAffineTransformationclass is needed here. First, shift the image half its size to the upper left, then rotate, and shift the result back to the lower right, possibly using the new image dimensions.GDI+
Introduced in Windows XP, Microsoft’s GDI+ API is more efficient then the default GDI API. For Delphi 2009 and up, the library is available from here. For older Delphi versions, the library is available from here.
In GDI+ the rotation is also done by a transformation matrix. Drawing works quite differently though. Create a
TGPGraphicsobject and attach it to a device context with its constructor. Subsequently, drawing operations on the object are translated by the API and will be output to the destination context.Handling transparency
The routines above preserve the transparent settings of the fead bitmap, with the exception of the Graphics32 solution which requires an additional
Transparentparameter.Performance and image quality
I wrote a test application (see full code below) to tune the performance of the various methods and to compare the resulting image quality.
The first and most important conclusion is that GDI+ uses anti-aliasing where the others do not, resulting in the best image quality. (I unsuccessfully tried to prevent anti-aliasing by setting
CompositingQuality,InterpolationMode,SmoothingMode, andPixelOffsetMode, so when anti-aliasing is not preferred, do not use GDI+.)Furthermore, the GDI+ solution is also the fastest method, by far.