HOW TO:裁剪圖形

更新:2007 年 11 月

本範例示範如何為 Drawing 定義裁剪區域。

使用 DrawingGroup 來定義 Drawing 的裁剪。DrawingGroup 類別是唯一能讓您定義自己裁剪區域的一種 Drawing 物件。

使用 Geometry 說明此裁剪,定將它套用至 DrawingGroup 物件的 ClipGeometry 屬性。

範例

下圖顯示套用橢圓形裁剪之前和之後的 DrawingGroup

具有已定義之裁剪區域的 DrawingGroup

下列範例會使用 DrawingGroup,將 ClipGeometry 套用至多個 GeometryDrawing 物件。

<Page 
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:PresentationOptions="https://schemas.microsoft.com/winfx/2006/xaml/presentation/options" 
  xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="PresentationOptions"
  Background="White" Margin="20">

  <Border BorderBrush="Gray" BorderThickness="1" 
    HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="20">
    <Image Stretch="None" HorizontalAlignment="Left">
      <Image.Source>
        <DrawingImage PresentationOptions:Freeze="True">
          <DrawingImage.Drawing>

            <!-- A DrawingGeometry with an elliptical clip region. -->
            <DrawingGroup>
              <GeometryDrawing Brush="Pink">
                <GeometryDrawing.Geometry>
                  <RectangleGeometry Rect="0,0,50,85" />
                </GeometryDrawing.Geometry>
              </GeometryDrawing>
              <GeometryDrawing Brush="Lime" 
                Geometry="M 25,25 L 0,50 25,75 50,50 25,25 25,0">
                <GeometryDrawing.Pen>
                  <Pen Thickness="10" Brush="Black" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
              <GeometryDrawing Brush="Lime">
                <GeometryDrawing.Geometry>
                  <EllipseGeometry Center="10,10" RadiusX="5" RadiusY="5" />
                </GeometryDrawing.Geometry>
                <GeometryDrawing.Pen>
                  <Pen Thickness="2" Brush="Black" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>

              <DrawingGroup.ClipGeometry>
                <EllipseGeometry Center="25,50" RadiusX="25" RadiusY="50" />
              </DrawingGroup.ClipGeometry>
            </DrawingGroup>
          </DrawingImage.Drawing>
        </DrawingImage>
      </Image.Source>
    </Image>
  </Border>


</Page>

請參閱

概念

繪圖物件概觀

幾何概觀

參考

Geometry

Freeze

PresentationOptions:Freeze 屬性