2011-07-24 32 views
5

Trong WPF tôi có một hình ảnh, có cách nào để áp dụng ví dụ các tọa độ này (bản địa cho hình ảnh): 0,10 20,0 20,20 để chọn lọc chỉ hiển thị tam giác được hình thành bởi những tọa độ đó.Slice image wpf

Dựa trên lời khuyên tốt của H.B. Tôi có thể làm mờ mặt nạ nhưng tôi không thể đặt bản đồ đúng cách, cũng không cắt để vừa với mặt nạ mờ. Ví dụ dưới đây được cho là qua Illinois

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

Trả lời

2

Bạn có thể tạo ra hình tam giác này là OpacityMask hoặc Clip hình ảnh của bạn.

ví dụ:

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

Bạn có thể cắt các không gian hình ảnh bằng cách sử dụng margin:

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

Bộ chuyển đổi:

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

này là khó có một giải pháp tốt đẹp, nhưng nó có vẻ làm việc ...

+0

Mặt nạ Opacity dường như không hỗ trợ có điều gì đó tôi thiếu – maxfridbe

+0

Bạn có thể tạo đường dẫn mặc dù, xem ví dụ tôi đã thêm vào. –

+0

Thats tuyệt vời, chỉ cần một câu hỏi nữa, Có cách nào để làm cho nó chỉ mất giới hạn đường dẫn. tức là 20x20 thay vì kích thước hình ảnh đầy đủ? Cảm ơn – maxfridbe