tags:

views:

424

answers:

4

How would I go about rotating a Bitmap in Windows GDI,C++?

+1  A: 

Sounds like you have to use PlgBlt. Take your rectangle's 4 corners as 2D Points, rotate them, then call PlgBlt.

From MSDN Bitmap Rotation:

To copy a bitmap into a parallelogram; use the PlgBlt function, which performs a bit-block transfer from a rectangle in a source device context into a parallelogram in a destination device context. To rotate the bitmap, an application must provide the coordinates, in world units, to be used for the corners of the parallelogram.

Frank Krueger
The PlgBlt docs specifically mention that rotation transformations are not supported. That's accurate. Link: http://msdn.microsoft.com/en-us/library/dd162804%28VS.85%29.aspx
Hans Passant
nobugz: Please read the post and the link before voting something down. I am suggesting that he pass in a **pre-rotated** parallelogram, just as the docs say to do. PlgBlt does not support **rotation transformations on the context**. That means that the context rotation won't be applied. **But he can apply it himself to the rectangle to get a parallelogram**.
Frank Krueger
I didn't vote you down.
Hans Passant
+3  A: 

You can do it with GDI+ (#include <gdiplus.h>). The Graphics class has the RotateTransform method. That allows arbitrary rotations. Use Image::RotateFlip() if you only need to rotate by 90 degree increments, that's a lot more efficient.

Hans Passant
I thought he asked for GDI?
Frank Krueger
@Frank Krueger - you have to be able to mind-read on this site. Either that, or throw pasta at the wall and see what sticks.
Mark Ransom
A: 

Another possibility (beyond those already suggested) is to use SetWorldTransform(). This is different in that it is modal and applies to the DC as a whole, not just a single operation. If you want to rotate one bitmap rotated, but other things without rotation, it's probably not your best choice. If you want to draw a number of things rotated, or (especially) if you want to rotate everything you draw (at least into one DC) it can work quite nicely though.

Jerry Coffin
I tried SetWorldTransform () and it had absolutely no effect. The documentation says that the device context's graphics mode must be set to GM_ADVANCED. I tried it, and it resulted in a totally black bitmap.This is what usually happens when trying a Microsoft feature; it doesn't work, and you spend the other 90% of your time trying to figure out why. .NET is noticeably better, but it's so slow.
+1  A: 

I saw a promising and efficient solution that only uses GDI here: http://www.codeguru.com/cpp/g-m/gdi/print.php/c3693

Haven't tried it yet.