Imagebrush 正在拉伸图像的一个实例而不是另一个实例

Imagebrush is stretching one instance of an image but not another

我遇到过一个奇怪的事件,我有一组使用特定图像作为背景的重复按钮。尽管按钮相同,但显示方式不同。下面是完全可重现的示例,以及许多具有不同结果的变体。

Link 到图标:

https://github.com/driftyco/ionicons/blob/master/png/512/plus.png https://github.com/driftyco/ionicons/blob/master/png/512/minus.png

<Grid Margin="0,0,0,0" >
    <StackPanel Orientation="Horizontal" Height="32">
        <StackPanel.Resources>
            <ImageBrush x:Key="plusImage" ImageSource="/Resources/plus.png" />
            <ImageBrush x:Key="minusImage" ImageSource="/Resources/minus.png" />
        </StackPanel.Resources>
        <RepeatButton Width="20"
              Height="20"
              Background="{Binding Source={StaticResource minusImage}}"/>
        <TextBlock Margin="5,0,5,0"
           VerticalAlignment="Top"
           FontWeight="Bold"
           Text="X" />
        <RepeatButton Width="20"
              Height="20"
              Background="{Binding Source={StaticResource plusImage}}"/>
        <Separator Margin="5,0,5,0" Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
        <RepeatButton Width="20"
              Height="20"
              Background="{Binding Source={StaticResource minusImage}}"/>
        <TextBlock Margin="5,0,5,0"
           VerticalAlignment="Top"
           FontWeight="Bold"
           Text="Y" />
        <RepeatButton Width="20"
              Height="20"
              Background="{Binding Source={StaticResource plusImage}}"/>
        <Separator Margin="5,0,5,0" Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
        <RepeatButton Width="20"
              Height="20"
              Background="{Binding Source={StaticResource minusImage}}"/>
        <TextBlock Margin="5,0,5,0"
           VerticalAlignment="Top"
           FontWeight="Bold"
           Text="Z" />
        <RepeatButton Width="20"
              Height="20"
              Background="{Binding Source={StaticResource plusImage}}"/>
    </StackPanel>
</Grid>

我正在创建一个控件来控制点的 x、y、z 位置。正如您在上图中看到的,只有一个加号图标看起来被拉伸了。

变化:

什么会导致画笔像这样拉伸图像?

您可以在 RepeatButtons 上设置 UseLayoutRounding 属性:

<RepeatButton Width="20" Height="20" UseLayoutRounding="True"
              Background="{Binding Source={StaticResource plusImage}}"/>