【问题标题】:Slice image wpf切片图像 wpf
【发布时间】:2011-07-24 20:43:53
【问题描述】:

在 WPF 中我有一个图像,有没有办法应用例如这些坐标(图像原生): 0,10 20,0 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>

【问题讨论】:

    标签: c# wpf image xaml


    【解决方案1】:

    您可以将此三角形创建为图像的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>
    

    您可以使用边距裁剪图像空间:

    <Image.Margin>
        <Binding Path="Clip.Bounds" RelativeSource="{RelativeSource Self}">
            <Binding.Converter>
                <vc:BoundsToMarginConverter />
            </Binding.Converter>
        </Binding>
    </Image.Margin>
    

    转换器:

    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();
        }
    }
    

    这不是一个好的解决方案,但它似乎工作......

    【讨论】:

    • 不透明蒙版似乎不支持 我缺少什么
    • 您可以创建路径,请参阅我添加的示例。
    • 太棒了,还有一个问题,有没有办法让它只占用路径边界。即 20x20 而不是完整的图像尺寸?谢谢
    • 嗯,好问题,我认为Clip(无论如何这是更合适的属性)可能会自动执行此操作,但看起来不像,可能可以通过绑定执行某些操作,可能需要另一个容器...
    • 我测试了一些东西并找到了一个使用 Margin 的选项,请参阅我更新的答案...(除非您将对齐设置为 CenterStretch 以外的其他内容,否则该展示位置可能不过有点奇怪)
    猜你喜欢
    • 2015-05-08
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 2017-11-15
    相关资源
    最近更新 更多