简体   繁体   中英

How to rotate an image within a StackPanel or Grid in Silverlight

I have an image control sits inside a Grid control. I already have a button to enable zoom-in to this image. After zoom-in, the Horizontal/vertical scroll bars are displayed. And then I rotate the image contained grid, the image and the grid scroll bar are messed up. How should I incorporate both zoom-in and rotate for the image control? The following are the code that I am using in my project.

The image control zoom-in code I used (x is the image control):

if ((x as Image) != null) { x.Height = x.Height * 1.3; x.Width = x.Width * 1.3; } 

The rotation code I used (x is the image control):

if ((x as Image) != null)
{    
    RotateTransform rotate = new RotateTransform(); rotate.Angle = rotateAngle;
    rotate.CenterX = x.Width / 2;
    rotate.CenterY = x.Height / 2;
    x.RenderTransform = rotate;                                       
};

The XAML is:

<ScrollViewer x:Name="scrollViewer" Height="480" Width="615"
                    VerticalScrollBarVisibility="Auto"
                    HorizontalScrollBarVisibility="Auto">
  <ScrollViewer.Content>
      <Grid x:Name="ImageGrid">
          <StackPanel x:Name="ImageStackPanel">
              <Image Source="..." VerticalAlignment="Center"  Width="220" Height="170" ></Image>  
          </StackPanel>
      </Grid>
  </ScrollViewer.Content>
</ScrollViewer>

Does anybody have any existing code snippet that I can borrow to resolve this trick?

I think you need to use TransformGroup to use more than one transform at the time:

        ScaleTransform myScaleTransform = new ScaleTransform();
        myScaleTransform.ScaleY = 3;

        RotateTransform myRotateTransform = new RotateTransform();
        myRotateTransform.Angle = 45;

        // Create a TransformGroup to contain the transforms 
        // and add the transforms to it.
        TransformGroup myTransformGroup = new TransformGroup();
        myTransformGroup.Children.Add(myScaleTransform);
        myTransformGroup.Children.Add(myRotateTransform);

        // Associate the transforms to the image.
        x.RenderTransform = myTransformGroup;

This may work for your needs:

  <Image x:Name="image" Source="myImageSource" Stretch="Uniform" 
               HorizontalAlignment="Center" VerticalAlignment="Center" 
               RenderTransformOrigin="0.5, 0.5">
                 <Image.RenderTransform>
                     <TransformGroup>
                         <RotateTransform x:Name="Rotate"/>
                         <ScaleTransform x:Name="Scale" />
                     </TransformGroup>
                 </Image.RenderTransform>
  </Image>

code behind:

  Rotate.Angle = 45;
  Scale = 0.25;

You may be missing the LayoutTransformer from the Silverlight Toolkit , and the AnimationMediator from one of the Toolkit developers .

With the LayoutTransformer you can set its content to anything, not just images, and apply any transformation with it, and as opposed to the usual RenderTransform , it will affect layout and actual sizes.

I have a similar scenario and I use it like this:

<Grid>
    <fs:AnimationMediator x:Name="RotateMediator" LayoutTransformer="{Binding ElementName=LayoutTransformer}" AnimationValue="{Binding Angle, ElementName=RotateTransform, Mode=TwoWay}" />
    <fs:AnimationMediator x:Name="ScaleXMediator" LayoutTransformer="{Binding ElementName=LayoutTransformer}" AnimationValue="{Binding ScaleX, ElementName=ScaleTransform, Mode=TwoWay}" />
    <fs:AnimationMediator x:Name="ScaleYMediator" LayoutTransformer="{Binding ElementName=LayoutTransformer}" AnimationValue="{Binding ScaleY, ElementName=ScaleTransform, Mode=TwoWay}" />

    <tkt:LayoutTransformer x:Name="LayoutTransformer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <tkt:LayoutTransformer.LayoutTransform>
            <TransformGroup>
                <RotateTransform x:Name="RotateTransform" />
                <ScaleTransform x:Name="ScaleTransform" />
            </TransformGroup>
        </tkt:LayoutTransformer.LayoutTransform>

        <Image x:Name="MyImage" Source="mysource.png" Width="600" Height="800" />

    </tkt:LayoutTransformer>
</Grid>

Because of the lack of MultiBinding you'd probably additionally have to manually handle the input value (from Slider controls etc) changed events and then set the AnimationValues of RotateMediator etc accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM