Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 缩放画布而不缩放画布内的图像_C#_Wpf_Image_Canvas_Graphics - Fatal编程技术网

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

C# 缩放画布而不缩放画布内的图像,c#,wpf,image,canvas,graphics,C#,Wpf,Image,Canvas,Graphics,我在画布上有一个图像,我将画布的大小设置为图像的大小(我希望画布尽可能小)。现在我想在图像上应用阴影效果,但有些部分会被剪裁,因为它们位于画布之外。所以我需要在不缩放图像的情况下增加画布 我可以手动缩小图像,但我宁愿不触摸图像,而是增加画布的大小 这是我尝试过的,但是图像会随着画布的变化而变化 <Canvas ClipToBounds="True" Name="ImageDropShadowThumbnail" Height="{Binding Height}" Width="{Bindi

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

我可以手动缩小图像,但我宁愿不触摸图像,而是增加画布的大小

这是我尝试过的,但是图像会随着画布的变化而变化

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


[值转换(typeof(double),typeof(double))]
公共类高度转换器:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
双重结果;
Double.TryParse(value.ToString(),out结果);
返回结果+20.0;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}

由于您将其放置在画布中,因此默认情况下它将放置在左上角,因此需要设置canvas.left/top等。

将转换器添加到画布宽度和高度绑定到图像大小,并在转换器中添加一些空间+30,例如,以便dropshadoweffect可见。
 <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();
    }
}