WPF以任意角度旋转图像

时间:2015-04-12 02:40:16

标签: c# wpf rotation

我正在尝试将BitmapImage myImage1和myImage2旋转到任意角度(可能是45,30,10等等)

我得到的错误是我只能将它旋转到90度,180度或270度。

XAML代码:

<Window x:Class="CSWPFAnimatedImage.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF Animated Image Sample" Height="300" Width="300" Loaded="Window_Loaded">
<Window.Resources>
    <Storyboard x:Key="VisibleToInvisible" Completed="VisbleToInvisible_Completed" >
        <DoubleAnimation Storyboard.TargetName="TransparentStop"
                        Storyboard.TargetProperty="Offset" To="0"  Duration="0:0:2"   />
        <DoubleAnimation Storyboard.TargetName="BlackStop"
                        Storyboard.TargetProperty="Offset" To="0" Duration="0:0:2"
                        />
    </Storyboard>
    <Storyboard x:Key="InvisibleToVisible" Completed="InvisibleToVisible_Completed">
        <DoubleAnimation Storyboard.TargetName="TransparentStop"
                        Storyboard.TargetProperty="Offset" To="1"  Duration="0:0:2"   />
        <DoubleAnimation Storyboard.TargetName="BlackStop"
                        Storyboard.TargetProperty="Offset" To="1" Duration="0:0:2"   />
    </Storyboard>
</Window.Resources>
<Window.Triggers>
    <EventTrigger RoutedEvent="Window.Loaded">
        <EventTrigger.Actions>
            <BeginStoryboard Storyboard="{StaticResource  VisibleToInvisible}"/>
        </EventTrigger.Actions>
    </EventTrigger>
</Window.Triggers>
<Grid Name="grid">        
    <Image x:Name="myImage2" Source="Images/image2.jpg" />
    <Image x:Name="myImage1" Source="Images/image1.jpg">
        <Image.OpacityMask>
            <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                <GradientStop Offset="1" Color="Black" x:Name="BlackStop"/>
                <GradientStop Offset="1" Color="Transparent" x:Name="TransparentStop"/>
            </LinearGradientBrush>
        </Image.OpacityMask>
    </Image>
</Grid> </Window>

以下是C#中的代码:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;

namespace CSWPFAnimatedImage
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    int nextImageIndex;
    List<BitmapImage> images = new List<BitmapImage>();

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        // Initialize the images collection
        images.Add(new BitmapImage(new Uri("Images/image1.jpg", UriKind.Relative)));
        images.Add(new BitmapImage(new Uri("Images/image2.jpg", UriKind.Relative)));
        images.Add(new BitmapImage(new Uri("Images/image3.jpg", UriKind.Relative)));
        images.Add(new BitmapImage(new Uri("Images/image4.jpg", UriKind.Relative)));

        nextImageIndex = 2;
    }

    private void VisbleToInvisible_Completed(object sender, EventArgs e)
    {
        // Change the source of the myImage1 to the next image to be shown
        // and increase the nextImageIndex
        this.myImage1.Source = images[nextImageIndex++];

        // If the nextImageIndex exceeds the top bound of the collection,
        // get it to 0 so as to show the first image next time
        if (nextImageIndex == images.Count)
        {
            nextImageIndex = 0;
        }

        // Get the InvisibleToVisible storyboard and start it
        Storyboard sb = this.FindResource("InvisibleToVisible") as Storyboard;
        sb.Begin(this);

    }

    private void InvisibleToVisible_Completed(object sender, EventArgs e)
    {
        // Change the source of the myImage2 to the next image to be shown
        // and increase the nextImageIndex
        this.myImage2.Source = images[nextImageIndex++];

        // If the nextImageIndex exceeds the top bound of the collection,
        // get it to 0 so as to show the first image next time
        if (nextImageIndex == images.Count)
        {
            nextImageIndex = 0;
        }

        // Get the VisibleToInvisible storyboard and start it
        Storyboard sb = this.FindResource("VisibleToInvisible") as Storyboard;
        sb.Begin(this);

        this.turnToAnyAngle(); // Make it rotate to angle set below
    }  

    private void turnToAnyAngle()
    {
        TransformedBitmap tb = new TransformedBitmap();

        var biRotated = new BitmapImage();
        biRotated.BeginInit();
        biRotated.UriSource = new Uri(@"INSERT_IMAGE_PATH_HERE", UriKind.RelativeOrAbsolute);
        biRotated.EndInit();

        tb.BeginInit();
        tb.Source = biRotated;
        RotateTransform transform = new RotateTransform();
        transform.Angle = 45;
        tb.Transform = transform;
        tb.EndInit();

        myImage2.Source = tb;

    }

}
}

在C#方法turnToAnyAngle()中,我收到以下错误:

  

类型'System.InvalidOperationException'的未处理异常   发生在PresentationCore.dll

     

附加信息:变换必须是尺度的组合,   翻转和90度旋转

我需要在不影响渲染的情况下旋转此图像,因此rendertransform不是一种可能的解决方案。

1 个答案:

答案 0 :(得分:0)

您只能使用TransformedBitmap以90度为增量旋转,请参阅here

您应该做的是在Image控件(托管位图)上执行RenderTranform,请参阅here