C# 如何在xaml中剪裁图像并限制图像宽度?(温特)
我有一个图像,可以通过一个api设置,我希望图像得到剪辑时,它的宽度超过250像素。这就行了。但是,图像与一些文本块一起位于stackpanel中。即使我们看到的图像被剪裁,实际的图像宽度仍然保持在250像素以上 这是xamlC# 如何在xaml中剪裁图像并限制图像宽度?(温特),c#,.net,xaml,windows-runtime,C#,.net,Xaml,Windows Runtime,我有一个图像,可以通过一个api设置,我希望图像得到剪辑时,它的宽度超过250像素。这就行了。但是,图像与一些文本块一起位于stackpanel中。即使我们看到的图像被剪裁,实际的图像宽度仍然保持在250像素以上 这是xaml <StackPanel Orientation="Horizontal" VerticalAlignment="Center"> <Button Foreground="Black" Content="Bu
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Button Foreground="Black" Content="Button" x:Name="BackButton" Style="{StaticResource BackButtonStyle}" Visibility="Collapsed" VerticalAlignment="Center" Margin="25,0,0,0" Click="BackButtonClick" />
<Border>
<Image x:Name="LogoImage" Source="Images/Logo.png" Height="50" Margin="15 0 0 0" Stretch="Uniform" VerticalAlignment="Center">
<Image.Clip>
<RectangleGeometry Rect="0 0 50 250"></RectangleGeometry>
</Image.Clip>
</Image>
</Border>
<TextBlock Foreground="Black" x:Name="NameTextbox" Margin="15, 0, 0, 0" VerticalAlignment="Center" FontSize="26"></TextBlock>
<TextBlock VerticalAlignment="Bottom" x:Name="ErrorMessage" Text="Unable to reach server" Foreground="Red" Margin="15 0 0 0" FontSize="26" FontWeight="Bold" Visibility="Collapsed"></TextBlock>
</StackPanel>
假设图像的宽度是2000px。然后图像后的文本块将被推出屏幕,但只有250像素的图像可见
有什么建议吗?看来我采取了错误的方法。我能够使用禁用滚动的滚动查看器实现我想要的
<ScrollViewer MaxWidth="350" HorizontalScrollBarVisibility="Hidden" HorizontalScrollMode="Disabled">
<Image x:Name="LogoImage" HorizontalAlignment="Left" Source="Images/Logo.png" Height="50" Margin="15 0 0 0" Stretch="Uniform" VerticalAlignment="Center">
</Image>
</ScrollViewer>
您只需设置边框的宽度和高度,并将“图像拉伸”设置为“无”,避免使用沉重的ScrollViewer。我尝试了@FilipSkakun的答案,效果非常好,只需一次调整:我将Image.Stretch
设置为UniformToFill
我在这里发布我的代码,因为它可能会帮助某人:
<Border Width="30" Height="30">
<Border.Clip>
<EllipseGeometry RadiusX="15" RadiusY="15" Center="15,15" />
</Border.Clip>
<Image Source="..." VerticalAlignment="Center" MaxWidth="30" Stretch="UniformToFill" />
</Border>
另外,我可以通过
垂直对齐属性来控制图像定位。这对我不起作用,因为图像的高度不能保证为50像素。这是我需要的高度。因此,图像必须根据高度均匀拉伸。将“拉伸”(stretch)设置为“无”(none)只会剪裁图像的高度和宽度。好吧,但您仍然可以将边框剪裁到其边界,并使其内部的图像大于边框。您是否解决过此问题?我也有同样的问题。是的,我放弃了剪辑,使用了scrollviewer。如果禁用滚动,则它将剪辑图像并保留为scrollviewer设置的任何尺寸。实际上,我让滚动不可见,以防任何人试图滚动图像。我把代码贴在下面。