圖片剪下
今天我要實現在圖片上剪下我們想要的圖形。
首先建立一個WPF應用程式。
我們在圖片上實現剪下,那一定要用到Image控制項了。用到Image的剪下屬性Clip。
下面我們先截一個橢圓。
<Grid> <Image Source="tulips.jpg" Width="300" Height="200" Stretch="Fill"> <Image.Clip> <EllipseGeometry Center="150,100" RadiusX="120" RadiusY="90"/> </Image.Clip> </Image> </Grid>
效果
image.Clip下有6個可畫的圖形。
1、CombinedGeometry 表示由兩個 System.Windows.Media.Geometry 對象組合定義的二維幾何形狀。
2、GeometryGroup 表示由其他 System.Windows.Media.Geometry 對象組成的複合幾何圖形。
3、LineGeometry 表示線條的幾何圖形。
4、PathGeometry
指定用於產生此動畫輸出值的幾何圖形。這是一個依賴項屬性。
傳回值:
用於產生此動畫輸出值的路徑。預設值為 null。
5、RectangleGeometry 描述二維矩形。
6、StreamGeometry
定義幾何形狀,並使用 System.Windows.Media.StreamGeometryContext 進行描述。此幾何圖形是 System.Windows.Media.PathGeometry 的輕量替代圖形:它不支援資料繫結、動畫或修改。
下面我們用PathGeomitry來畫。
<Image Source="tulips.jpg" Width="300" Height="200" Stretch="Fill"> <Image.Clip> <PathGeometry> <PathGeometry.Figures> <PathFigureCollection> <PathFigure StartPoint="150,30" IsClosed="True"> <LineSegment Point="60,180" /> <LineSegment Point="240,180"/> </PathFigure> </PathFigureCollection> </PathGeometry.Figures> </PathGeometry> </Image.Clip> </Image>
這裡用到IsClosed=”True”是圖形封閉起來。