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

本文关键字:缩放 图像 布内 | 更新日期: 2023-09-27 18:21:16

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

我可以手动缩小图像,这很有效,但我宁愿不触摸图像,而是增加画布大小。

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

<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>

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

 <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等。