Thursday, December 20, 2012

Image Rotation inside WP7 Scrollviewer

Image Rotation inside WP7 Scrollviewer

In an Windows Phone 7 application, I want to apply a 90ยบ rotation to an image when the user clicks a button. I'm doing the following:

<Image Height="369" Name="imageView" Stretch="Uniform" Width="394">    <Image.RenderTransform>       <RotateTransform Angle="0" />    </Image.RenderTransform> </Image> 

At the function:

((RotateTransform)imageView.RenderTransform).Angle += 90; 

So far, so good. The problem appears when I place the image within a scrollviewer.

<ScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Visible"  ScrollViewer.VerticalScrollBarVisibility="Visible" Height="389" HorizontalAlignment="Left" Margin="12,78,0,0" Name="scrollViewer" VerticalAlignment="Top" Width="409">    <Image Height="369" Name="imageView" Stretch="Uniform" Width="394">       <Image.RenderTransform>          <RotateTransform Angle="0" />       </Image.RenderTransform>    </Image> </ScrollViewer> 

In this case I get an error - InvalidCastException was unhandled - on this line of code:

((RotateTransform)imageView.RenderTransform).Angle += 90; 

Answers & Comments...

Answer: 1

The problem is that imageView.RenderTransform is not a RotateTransform then, but of CompositeTransform instead, so try this instead:

((CompositeTransform)imageView.RenderTransform).Rotation += 90; 

And if you want to specify it as a Composite transform directly in the XAML too, you can do that:

<ScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Visible"  ScrollViewer.VerticalScrollBarVisibility="Visible" Height="389" HorizontalAlignment="Left" Margin="12,78,0,0" Name="scrollViewer" VerticalAlignment="Top" Width="409">     <StackPanel>          <Image Height="369" Name="imageView" Stretch="Uniform" Width="394" Source="/PhoneApp2;component/Images/lumia-920-rainbow.png">             <Image.RenderTransform>                 <CompositeTransform Rotation="0" CenterX="197" CenterY="184" />             </Image.RenderTransform>         </Image>     <StackPanel> </ScrollViewer> 
by : Johan Falkhttp://stackoverflow.com/users/1330240




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog