切片图像wpf

本文关键字:wpf 图像 切片 | 更新日期: 2023-09-27 18:03:26

在WPF中我有一个图像,是否有一种方法来应用例如这些坐标(图像的原生):0,10,20,20,20有选择地只显示由这些坐标组成的三角形。

基于H.B.好的建议,我可以不透明遮罩,但我不能正确地放置地图,也不能裁剪以适应不透明遮罩。下面的例子应该在伊利诺斯州上空

 <Image Source="http://www.digital-topo-maps.com/county-map/united-states-map.gif" Stretch="None">
    <Image.Clip>
        <PathGeometry>
            <PathFigure StartPoint="444.806216983824,129.344961240310"  IsClosed="True" IsFilled="True">
                <LineSegment Point="445.976759660097,145.147286821705"/>
                <LineSegment Point="431.344976206682,170.313953488372"/>
                <LineSegment Point="447.732573674507,188.457364341085"/>
                <LineSegment Point="458.852729099102,213.038759689923"/>
                <LineSegment Point="469.387613185561,214.209302325581"/>
                <LineSegment Point="481.093039948293,191.383720930233"/>
                <LineSegment Point="479.337225933884,143.391472868217"/>
                <LineSegment Point="477.581411919474,132.271317829457"/>
                <LineSegment Point="444.806216983824,129.344961240310"/>
            </PathFigure>
        </PathGeometry>
    </Image.Clip>
</Image>

切片图像wpf

您可以创建这个三角形作为您的图像的OpacityMaskClip

<Image.Clip>
    <PathGeometry>
        <PathFigure StartPoint="0,10">
            <LineSegment Point="20,0" />
            <LineSegment Point="20,20" />
        </PathFigure>
    </PathGeometry>
</Image.Clip>
<Image.OpacityMask>
    <VisualBrush Stretch="None" AlignmentX="Left" AlignmentY="Top">
        <VisualBrush.Visual>
            <Path Fill="Black">
                <Path.Data>
                    <PathGeometry>
                        <PathFigure StartPoint="0,10" IsClosed="True" IsFilled="True">
                            <LineSegment Point="20,0" />
                            <LineSegment Point="20,20" />
                        </PathFigure>
                    </PathGeometry>
                </Path.Data>
            </Path>
        </VisualBrush.Visual>
    </VisualBrush>
</Image.OpacityMask>

你可以使用margin:

来裁剪图像空间
<Image.Margin>
    <Binding Path="Clip.Bounds" RelativeSource="{RelativeSource Self}">
        <Binding.Converter>
            <vc:BoundsToMarginConverter />
        </Binding.Converter>
    </Binding>
</Image.Margin>
converter:

public class BoundsToMarginConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Rect input = (Rect)value;
        return new Thickness()
        {
            Left = -input.Left,
            Right = -input.Right,
            Top = -input.Top,
            Bottom = -input.Bottom,
        };
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}