简体   繁体   English

如何在xaml中剪切图像并限制图像宽度? (WinRT的)

[英]How do I clip an image in xaml and limit the image width? (Winrt)

I have an image that can get set through an api, I want the image to get clipped when it's wider than 250 px. 我有一个可以通过api设置的图像,我希望图像在宽度超过250像素时被剪裁。 And that works. 这很有效。 However, the image is in a stackpanel along with some textblocks. 但是,图像与一些文本块一起位于堆栈面板中。 And even though what we see of the image is clipped, the actual image width remains over 250 px. 即使我们看到的图像被裁剪,实际图像宽度仍然超过250像素。

Here is the xaml 这是xaml

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

So let's say that the image width is 2000 px. 所以我们说图像宽度是2000像素。 Then the textblock after the image will get pushed off of the screen, but only 250 pixels of the image will be visible. 然后,图像之后的文本块将被推离屏幕,但只能看到250像素的图像。

Any advice? 有什么建议?

It looks like I was taking the wrong approach. 看起来我采取了错误的做法。 I was able to achieve what I wanted using a scrollviewer with scrolling disabled. 我能够使用滚动查看器来禁用滚动来实现我想要的效果。

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

您可以设置边框的宽度和高度,并将Image Stretch设置为None,并避免使用较重的ScrollViewer。

I've tried @FilipSkakun's answer and it worked very well, with one adjustment: I put Image.Stretch to UniformToFill . 我已经尝试了@ FilipSkakun的答案并且它运行良好,只进行了一次调整:我将Image.Stretch放到UniformToFill

I'm posting my code here, as it may help someone: 我在这里发布我的代码,因为它可以帮助某人:

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

Plus, I can control the image positioning by the VerticalAlignment property. 另外,我可以通过VerticalAlignment属性控制图像定位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM