25

This has been bothering me for ages, I just want a simple method that rotates an image X degrees. (this is for a turret defense game in which the turrets need to shoot a certain direction)

I want something like this:

public Image getRotatedImage(Image img, float angle)
{
     //Code here to rotate the image
     return img
}

All of this in c# wpf ofcourse and dynamically.. Hope you guys can help me out here :D

1 Answers1

90

Don't use code to change your image. Let WPF rotate it for you using a RotateTransform.

<Image ...>
    <Image.RenderTransform>
        <RotateTransform Angle="45" />
    </Image.RenderTransform>
</Image >

Or apply a RotateTransform to your image in code:

RotateTransform rotateTransform = new RotateTransform(45);
img.RenderTransform = rotateTransform;
Nathan A
  • 11,059
  • 4
  • 47
  • 63
  • 8
    Thank you man! You're the first to explain this without like 45 lines of code :) I would give you an upvote, but i don't have enough rep :P –  May 16 '14 at 15:59
  • Exactly what I needed, thanks! – BMB Aug 06 '15 at 23:03
  • Yeah, what the first reply said! ;) This is actually the only post that explains the angle change without animation! Thanks! – AbeyMarquez Aug 14 '15 at 23:35
  • Is there any way to do this continuously? This is exactly what I need. – Thomas Nov 06 '15 at 14:25
  • @Thomas, by *continuously* do you mean animation? If so, there is a lot of information out there on animation, but is not answered here. This answer was for a static rotation of an image or perhaps binding the rotation to a view model, but not true wpf animation. – Nathan A Nov 06 '15 at 15:38