C# 如何在WPF中旋转网格背景图像?

C# 如何在WPF中旋转网格背景图像?,c#,wpf,C#,Wpf,我想在WPF中旋转网格背景图像。我有图像旋转的动画代码。但当我在网格背景中实现时,图像不被接受,所以Imagebrush只被接受 <Grid.Background> <ImageBrush ImageSource="../Images/1.jpg" Stretch="UniformToFill" TileMode="Tile"/> </Grid.Background> 我无法在WPF中实现下面的动画代码 <Canva

我想在WPF中旋转网格背景图像。我有图像旋转的动画代码。但当我在网格背景中实现时,图像不被接受,所以Imagebrush只被接受

  <Grid.Background>
            <ImageBrush ImageSource="../Images/1.jpg" Stretch="UniformToFill" TileMode="Tile"/>
  </Grid.Background>

我无法在WPF中实现下面的动画代码

<Canvas ClipToBounds="True" >
                <Image Source="/Images/1.jpg" Width="600"  >
                    <Image.RenderTransform>
                        <RotateTransform x:Name="TransRotate" />
                    </Image.RenderTransform>
                    <Image.Triggers>
                        <EventTrigger RoutedEvent="Image.Loaded">
                            <BeginStoryboard>
                                <Storyboard TargetProperty="Angle">
                                    <DoubleAnimation Storyboard.TargetName="TransRotate" Storyboard.TargetProperty="Angle" By="40" Duration="0:0:10" AutoReverse="True" RepeatBehavior="Forever" />
                                    <DoubleAnimation Storyboard.TargetName="TransRotate" Storyboard.TargetProperty="Angle" By="-40" Duration="0:0:15" AutoReverse="True" RepeatBehavior="Forever" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </Image.Triggers>
                </Image>
            </Canvas>

编辑1-

如果将图像设置为网格中的内容,则第0行仅显示图像动画

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Canvas ClipToBounds="True" > 
        <Image Name="logo" Source="/Images/1.jpg" Width="800"  >
            <Image.RenderTransform>
                <RotateTransform x:Name="TransRotate" />
            </Image.RenderTransform>
            <Image.Triggers>
                <EventTrigger RoutedEvent="Image.Loaded">
                    <BeginStoryboard>
                        <Storyboard TargetProperty="Angle">
                            <DoubleAnimation Storyboard.TargetName="TransRotate" Storyboard.TargetProperty="Angle" By="40" Duration="0:0:10" AutoReverse="True" RepeatBehavior="Forever" />
                            <DoubleAnimation Storyboard.TargetName="TransRotate" Storyboard.TargetProperty="Angle" By="-40" Duration="0:0:15" AutoReverse="True" RepeatBehavior="Forever" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Image.Triggers>
        </Image>
        </Canvas>

        <Border Grid.Row="0" Height="180" >
            <Image Source="Images/01.jpg" Height="100" />
        </Border>

        <Border Grid.Row="1" Height="180">
            <Image Source="Images/01.jpg" Height="100" />
        </Border>

        <Border Grid.Row="2" Height="180">
            <Image Source="Images/01.jpg" Height="100" />
        </Border>
    </Grid>


正如您所想,网格。背景只能用画笔。解决方案是将图像作为子对象添加到网格控件:

<Grid>
    <Image ... />
    ... grid child elements ...
<Grid>

... 网格子元素。。。

您需要将图像元素保留为网格的第一个子元素,以便它显示为网格背景,并注意可能出现的其他问题(如子元素的顺序、网格行/列的数量等),但您应该能够获得想要的效果。

如您所想,网格。背景只能使用笔刷。解决方案是将图像作为子对象添加到网格控件:

<Grid>
    <Image ... />
    ... grid child elements ...
<Grid>

... 网格子元素。。。

您需要将图像元素保留为网格的第一个子元素,以便它显示为网格背景,并注意可能出现的其他问题(如子元素的顺序、网格行/列的数量等),但您应该能够获得想要的效果。

如您所想,网格。背景只能使用笔刷。解决方案是将图像作为子对象添加到网格控件:

<Grid>
    <Image ... />
    ... grid child elements ...
<Grid>

... 网格子元素。。。

您需要将图像元素保留为网格的第一个子元素,以便它显示为网格背景,并注意可能出现的其他问题(如子元素的顺序、网格行/列的数量等),但您应该能够获得想要的效果。

如您所想,网格。背景只能使用笔刷。解决方案是将图像作为子对象添加到网格控件:

<Grid>
    <Image ... />
    ... grid child elements ...
<Grid>

... 网格子元素。。。

您需要将图像元素保留为网格的第一个子元素,以便它显示为网格背景,并注意可能出现的其他问题(如子元素的顺序、网格行/列的数量等),但是您应该能够获得想要的效果。

您可以像下面的示例中那样旋转图像笔刷。您可能还需要设置RotateTransform的
CenterX
CenterY
属性

<Grid>
    <Grid.Background>
        <ImageBrush ImageSource="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"
                    Stretch="UniformToFill">
            <ImageBrush.Transform>
                <RotateTransform/>
            </ImageBrush.Transform>
        </ImageBrush>
    </Grid.Background>
    <Grid.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation
                        Storyboard.TargetProperty="Background.Transform.Angle"
                        By="40" Duration="0:0:10"
                        AutoReverse="True" RepeatBehavior="Forever"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Grid.Triggers>
</Grid>

您可以像下面的示例中那样旋转图像笔刷。您可能还需要设置RotateTransform的
CenterX
CenterY
属性

<Grid>
    <Grid.Background>
        <ImageBrush ImageSource="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"
                    Stretch="UniformToFill">
            <ImageBrush.Transform>
                <RotateTransform/>
            </ImageBrush.Transform>
        </ImageBrush>
    </Grid.Background>
    <Grid.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation
                        Storyboard.TargetProperty="Background.Transform.Angle"
                        By="40" Duration="0:0:10"
                        AutoReverse="True" RepeatBehavior="Forever"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Grid.Triggers>
</Grid>

您可以像下面的示例中那样旋转图像笔刷。您可能还需要设置RotateTransform的
CenterX
CenterY
属性

<Grid>
    <Grid.Background>
        <ImageBrush ImageSource="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"
                    Stretch="UniformToFill">
            <ImageBrush.Transform>
                <RotateTransform/>
            </ImageBrush.Transform>
        </ImageBrush>
    </Grid.Background>
    <Grid.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation
                        Storyboard.TargetProperty="Background.Transform.Angle"
                        By="40" Duration="0:0:10"
                        AutoReverse="True" RepeatBehavior="Forever"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Grid.Triggers>
</Grid>

您可以像下面的示例中那样旋转图像笔刷。您可能还需要设置RotateTransform的
CenterX
CenterY
属性

<Grid>
    <Grid.Background>
        <ImageBrush ImageSource="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"
                    Stretch="UniformToFill">
            <ImageBrush.Transform>
                <RotateTransform/>
            </ImageBrush.Transform>
        </ImageBrush>
    </Grid.Background>
    <Grid.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation
                        Storyboard.TargetProperty="Background.Transform.Angle"
                        By="40" Duration="0:0:10"
                        AutoReverse="True" RepeatBehavior="Forever"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Grid.Triggers>
</Grid>


首先,我尝试您所说的,先生,但图像仅显示一行。第二排没有显示。我很快更新了问题部分的代码。我更新了我的问题,先生。看一看并给出解决方案。@Andrei Pana先生,我先试试你说的,但图片只显示了一行。第二排没有显示。我很快更新了问题部分的代码。我更新了我的问题,先生。看一看并给出解决方案。@Andrei Pana先生,我先试试你说的,但图片只显示了一行。第二排没有显示。我很快更新了问题部分的代码。我更新了我的问题,先生。看一看并给出解决方案。@Andrei Pana先生,我先试试你说的,但图片只显示了一行。第二排没有显示。我很快更新了问题部分的代码。我更新了我的问题,先生。请看一看并给出解决方案。@Andrei Pana如果
With
Height
未知,您也可以使用
ImageBrush.RelativeTransform
并将
CenterX
CenterY
设置为0.5以绕其中心旋转<代码>情节提要.TargetProperty随后将更改为
背景.RelativeTransform.Angle
如果
With
Height
未知,您也可以使用
ImageBrush.RelativeTransform
并将
CenterX设置为