缩放画布而不在画布内缩放图像

时间:2014-09-09 12:25:24

标签: c# wpf image canvas graphics

我在画布上有一个图像,我将画布的大小设置为图像的大小(我希望画布尽可能小)。现在我想对图像应用阴影效果,但是某些部分因为它们位于画布之外而被剪裁。所以我需要在不缩放图像的情况下增加画布。

我能够手动缩放图像,但是我不想触摸图像而是增加画布大小。

这是我尝试过的,但图像会随着画布缩放。

<Canvas ClipToBounds="True" Name="ImageDropShadowThumbnail" Height="{Binding Height}" Width="{Binding Width}">
    <Canvas.LayoutTransform>
        <ScaleTransform ScaleX="{Binding Value, ElementName=ScaleXSlider}" ScaleY="{Binding Value, ElementName=ScaleYSlider}" CenterX="{Binding CenterX}" CenterY="{Binding CenterY}"/>
    </Canvas.LayoutTransform>
    <Image Source="{Binding Name}" Height="{Binding Height}" Width="{Binding Width}" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Image.Effect>
            <DropShadowEffect BlurRadius="{Binding Value, ElementName=BlurRadiusSlider}" Direction="{Binding Value, ElementName=DirectionSlider}" Opacity="{Binding Value, ElementName=OpacitySlider}" ShadowDepth="{Binding Value, ElementName=ShadowDepthSlider}"/>
        </Image.Effect>
    </Image>

</Canvas>

1 个答案:

答案 0 :(得分:1)

 <Canvas Background="CadetBlue" Width="{Binding ElementName=Image1, Path=Width, Converter={StaticResource HeightConverter}}"
        Height="{Binding ElementName=Image1, Path=Height, Converter={StaticResource HeightConverter}}">
    <Image Name="Image1" Source="1.jpg" Width="150" Height="150" Stretch="Fill"/>
</Canvas>

 [ValueConversion(typeof(double), typeof(double))]
public class HeightConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double result;
        Double.TryParse(value.ToString(), out result);
        return result + 20.0;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

因为你把它放在画布上,它将默认放在左上角,所以需要设置Canvas.Left / Top等。