If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Windows SDK GDI: How do I display a bitmap rotated?

Q: How do I display a bitmap rotated?

A: Mirroring or inverting a bitmap about its center point is not difficult to achieve since they do not change its rectangular shape with horizontal and vertical sides. Rotating the bitmap in multiples of 90 degrees also is easy. But displaying a bitmap rotated by an arbitrary angle is difficult since the shape is changed and screen resolutions come into picture. Here I have attempted to achieve this, using simple graphic transformation.

I am presenting a function 'MBRotate()', which takes, as inputs, the handle to a display device context, X and Y positions of the top left corner where the bitmap is to be displayed, handle to the bitmap object and angle of rotation in radians. Rotation direction is positive in anti-clockwise direction and point about which the rotation is performed is the insertion point of the bitmap, which is the top left corner of the bitmap. Since I am using trigonometric functions like 'sin()' and 'cos()' functions for coordinate transformations, I have included '' header file. I have written two simple functions to return rotated coordinates of a point and an array of points, latter using the former function. Steps to display the bitmap rotated involves two main steps:

Get the bitmap dimensions and compute the span when it is rotated. Get minimum and maximum of X and Y coordinates.

Scans all the points from between the minimum and maximum values of X and Y. Checks whether the point corresponds to a point within the bitmap dimensions. If it is, pick up the corresponding pixel of the bitmap and display.

Here is what I got when I rotated the bitmap by 30 degrees. Point about which rotation is performed is marked for illustration.

For bigger bitmaps, 'GetPixel()'/'SetPixel()' calls will be time consuming. This can be changed by getting the pixel information of the bitmap in an array and reconstructing the rotated pixels from this information. That is beyond the scope of this post since this attempts to illustrate the method to display rotated bitmaps.