2011-07-24 69 views
5

在WPF中我有一個圖像,有沒有辦法申請例如這些座標(原生圖像): 0,10 20,0 20,20 有選擇地只顯示形成的三角形由那些座標。切片圖像wpf

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

回答

2

您可以創建這個三角形的OpacityMask或圖像的Clip

例如

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

這很難說是一個很好的解決方案,但它似乎

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

你可以使用保證金裁剪圖像空間工作...

+0

不透明度面具似乎不支持有什麼我失蹤 – maxfridbe

+0

雖然您可以創建一個路徑,請參閱我添加的示例。 –

+0

這真棒,只有一個問題,有沒有辦法讓它只佔用路徑邊界。即20x20,而不是完整的圖像大小?謝謝 – maxfridbe