WPF Drawing 對象概述


Drawing 對象描述一些可見內容,例如形狀、位圖、視頻或一行文本。 不同類型的 Drawing 描繪的是不同類型的內容。 下面列出了不同類型的 Drawing 對象。

Drawing 是一個通用對象;您可以通過多種方式使用 Drawing 對象。

WPF 提供了其他類型的對象,這些對象能夠繪制形狀、位圖、文本和媒體。 例如,您也可以使用 Shape 對象繪制形狀, MediaElement 控件提供了將視頻添加到應用程序中的另一種方式。 那么,什么時候應使用 Drawing 對象? 當可以犧牲框架級別功能以獲取性能優勢或需要 Freezable 功能時。 由於 Drawing 對象缺少對 布局、輸入和焦點的支持,因此它們具有性能優勢,是描述背景、剪貼畫以及使用 Visual 對象進行低級繪制的理想選擇。

由於 Drawing 對象是一種類型為 Freezable 的對象,因此它們具有若干特殊功能,其中包括可聲明為 資源、可在多個對象之間共享、可設為只讀以提高性能、可進行克隆以及可設為線程安全對象。 有關 Freezable 對象提供的不同功能的更多信息,請參見 Freezable 對象概述

 
 
繪制形狀

您可以使用 GeometryDrawing 來繪制形狀。 幾何繪圖的 Geometry 屬性描述要繪制的形狀,其 Brush 屬性描述形狀內部的繪制方式,其 Pen 屬性描述其輪廓的繪制方式。

下面的示例使用 GeometryDrawing 繪制形狀。 該形狀由 GeometryGroup 和兩個 EllipseGeometry 對象進行描述。 形狀內部使用 LinearGradientBrush 進行繪制,其輪廓則使用 Black  Pen 進行繪制。

本示例創建以下 GeometryDrawing

GeometryDrawing

兩個橢圓的 GeometryDrawing
  1. //
  2. // Create the Geometry to draw.
  3. //
  4. GeometryGroup ellipses = new GeometryGroup();
  5. ellipses.Children.Add(
  6.     new EllipseGeometry(new Point(50,50), 4520)
  7.     );
  8. ellipses.Children.Add(
  9.     new EllipseGeometry(new Point(5050), 2045)
  10.     );
  11.  
  12.  
  13. //
  14. // Create a GeometryDrawing.
  15. //
  16. GeometryDrawing aGeometryDrawing = new GeometryDrawing();
  17. aGeometryDrawing.Geometry = ellipses;
  18.  
  19. // Paint the drawing with a gradient.
  20. aGeometryDrawing.Brush = 
  21.     new LinearGradientBrush(
  22.         Colors.Blue, 
  23.         Color.FromRgb(204,204,255), 
  24.         new Point(0,0), 
  25.         new Point(1,1));
  26.  
  27. // Outline the drawing with a solid color.
  28. aGeometryDrawing.Pen = new Pen(Brushes.Black, 10);
  29.  

<GeometryDrawing>
  <GeometryDrawing.Geometry>

    <!-- Create a composite shape. -->
    <GeometryGroup>
      <EllipseGeometry Center="50,50" RadiusX="45" RadiusY="20" />
      <EllipseGeometry Center="50,50" RadiusX="20" RadiusY="45" />
    </GeometryGroup>
  </GeometryDrawing.Geometry>
  <GeometryDrawing.Brush>

    <!-- Paint the drawing with a gradient. -->
    <LinearGradientBrush>
      <GradientStop Offset="0.0" Color="Blue" />
      <GradientStop Offset="1.0" Color="#CCCCFF" />
    </LinearGradientBrush>
  </GeometryDrawing.Brush>
  <GeometryDrawing.Pen>

    <!-- Outline the drawing with a solid color. -->
    <Pen Thickness="10" Brush="Black" />
  </GeometryDrawing.Pen>
</GeometryDrawing>

有關完整的示例,請參見 如何:創建 GeometryDrawing

使用其他 Geometry 類(例如 PathGeometry),您可以通過創建曲線和弧線來創建更復雜的形狀。 有關 Geometry 對象的更多信息,請參見 Geometry 概述

有關不使用 Drawing 對象繪制形狀的其他方法的更多信息,請參見 WPF 中的形狀和基本繪圖概述

 
 
繪制圖像

您可以使用 ImageDrawing 繪制圖像。 ImageDrawing 對象的 ImageSource 屬性描述要繪制的圖像,其 Rect 屬性定義繪制圖像的區域。

下面的示例將在一個矩形的 (75,75) 處繪制一個大小為 100 x 100 像素的圖像。 下面的插圖顯示在示例中創建的 ImageDrawing 添加了一條灰色邊框,以顯示 ImageDrawing 的邊界。

大小為 100 x 100 的 ImageDrawing

在 (75,75) 處繪制的 100 x 100 ImageDrawing
  1. // Create a 100 by 100 image with an upper-left point of (75,75). 
  2. ImageDrawing bigKiwi = new ImageDrawing();
  3. bigKiwi.Rect = new Rect(7575100100);
  4. bigKiwi.ImageSource = new BitmapImage(
  5.     new Uri(@"sampleImages\kiwi.png", UriKind.Relative));
  6.  

<!-- The Rect property specifies that the image only fill a 100 by 100
     rectangular area. -->
<ImageDrawing Rect="75,75,100,100" ImageSource="sampleImages\kiwi.png"/>

有關圖像的更多信息,請參見 圖像處理概述

播放媒體(僅限代碼)
說明說明

雖然您可以在可擴展應用程序標記語言 (XAML) 中聲明 VideoDrawing,但是只能使用代碼加載並播放其媒體。 若要在可擴展應用程序標記語言 (XAML) 中播放視頻,請改用 MediaElement

您可以使用 VideoDrawing MediaPlayer 來播放音頻或視頻文件。 加載並播放媒體的方法有兩種。 第一種方法是使用 MediaPlayer VideoDrawing 自身,第二種方法是創建您自己的 MediaTimeline,並將其與 MediaPlayer VideoDrawing 一起使用。

說明說明

如果媒體隨應用程序一起分發,則不能像圖像那樣將媒體文件用作項目資源。 在項目文件中,必須將媒體類型改設為 Content,並將 CopyToOutputDirectory 設置為 PreserveNewestAlways

若要在不創建自己的 MediaTimeline 的情況下播放媒體,請執行下列步驟。

  1. 創建 MediaPlayer 對象。

    1. MediaPlayer player = new MediaPlayer();
    2.  
  2. 使用 Open 方法加載媒體文件。

    1. player.Open(new Uri(@"sampleMedia\xbox.wmv", UriKind.Relative));
    2.  
  3. 創建 VideoDrawing

    1. VideoDrawing aVideoDrawing = new VideoDrawing();
    2.  
  4. 通過設置 VideoDrawing Rect 屬性指定要繪制媒體的大小和位置。

    1. aVideoDrawing.Rect = new Rect(00100100);
    2.  
  5. 使用您創建的 MediaPlayer 來設置 VideoDrawing Player 屬性。

    1. aVideoDrawing.Player = player;
    2.  
  6. 使用 MediaPlayer Play 方法開始播放媒體。

    1. // Play the video once.
    2. player.Play();        
    3.  

下面的示例使用 VideoDrawing MediaPlayer 播放視頻文件一次。

  1. //
  2. // Create a VideoDrawing.
  3. //      
  4. MediaPlayer player = new MediaPlayer();
  5.  
  6. player.Open(new Uri(@"sampleMedia\xbox.wmv", UriKind.Relative));
  7.  
  8. VideoDrawing aVideoDrawing = new VideoDrawing();
  9.  
  10. aVideoDrawing.Rect = new Rect(00100100);
  11.  
  12. aVideoDrawing.Player = player;
  13.  
  14. // Play the video once.
  15. player.Play();        
  16.  
  17.  

若要對媒體進行更多的計時控制,請將 MediaTimeline MediaPlayer VideoDrawing 對象一起使用。 通過 MediaTimeline,您可以指定是否重復播放視頻。 若要將 MediaTimeline VideoDrawing 一起使用,請執行下列步驟:

  1. 聲明 MediaTimeline 並設置其計時行為。

    1. // Create a MediaTimeline.
    2. MediaTimeline mTimeline = 
    3.     new MediaTimeline(new Uri(@"sampleMedia\xbox.wmv", UriKind.Relative)); 
    4.  
    5. // Set the timeline to repeat.
    6. mTimeline.RepeatBehavior = RepeatBehavior.Forever;
    7.  
  2. MediaTimeline 創建 MediaClock

    1. // Create a clock from the MediaTimeline.
    2. MediaClock mClock = mTimeline.CreateClock();
    3.  
  3. 創建一個 MediaPlayer 並使用 MediaClock 設置其 Clock 屬性。

    1. MediaPlayer repeatingVideoDrawingPlayer = new MediaPlayer();
    2. repeatingVideoDrawingPlayer.Clock = mClock;
    3.  
  4. 創建一個 VideoDrawing,並將 MediaPlayer 分配給 VideoDrawing Player 屬性。

    1. VideoDrawing repeatingVideoDrawing = new VideoDrawing();
    2. repeatingVideoDrawing.Rect = new Rect(1500100100);
    3. repeatingVideoDrawing.Player = repeatingVideoDrawingPlayer;  
    4.  

下面的示例同時使用 MediaTimeline 以及 MediaPlayer VideoDrawing 來反復播放某視頻。

  1. //
  2. // Create a VideoDrawing that repeats.
  3. //
  4.  
  5. // Create a MediaTimeline.
  6. MediaTimeline mTimeline = 
  7.     new MediaTimeline(new Uri(@"sampleMedia\xbox.wmv", UriKind.Relative)); 
  8.  
  9. // Set the timeline to repeat.
  10. mTimeline.RepeatBehavior = RepeatBehavior.Forever;
  11.  
  12. // Create a clock from the MediaTimeline.
  13. MediaClock mClock = mTimeline.CreateClock();
  14.  
  15. MediaPlayer repeatingVideoDrawingPlayer = new MediaPlayer();
  16. repeatingVideoDrawingPlayer.Clock = mClock;
  17.  
  18. VideoDrawing repeatingVideoDrawing = new VideoDrawing();
  19. repeatingVideoDrawing.Rect = new Rect(1500100100);
  20. repeatingVideoDrawing.Player = repeatingVideoDrawingPlayer;  
  21.  

請注意,使用 MediaTimeline 時,使用從 MediaClock Controller 屬性返回的交互式 ClockController 控制媒體播放,而不是使用 MediaPlayer 的交互式方法。

繪制文本

您可以使用 GlyphRunDrawing GlyphRun 來繪制文本。 下面的示例使用 GlyphRunDrawing 來繪制文本“Hello World”。

  1. GlyphRun theGlyphRun = new GlyphRun(
  2.     new GlyphTypeface(new Uri(@"C:\WINDOWS\Fonts\TIMES.TTF")),
  3.     0,
  4.     false,
  5.     13.333333333333334,
  6.     new ushort[]{437279798235882857971},
  7.     new Point(012.29),
  8.     new double[]{
  9.         9.626666666666677.413333333333332.96
  10.         2.967.413333333333333.70666666666667
  11.         12.58666666666677.41333333333333
  12.         4.442.967.41333333333333},
  13.     null,
  14.     null,
  15.     null,
  16.     null,
  17.     null,
  18.     null
  19.  
  20.  
  21.     );
  22.  
  23. GlyphRunDrawing gDrawing = new GlyphRunDrawing(Brushes.Black, theGlyphRun);
  24.  

<GlyphRunDrawing ForegroundBrush="Black">
  <GlyphRunDrawing.GlyphRun>
    <GlyphRun 
      CaretStops="{x:Null}" 
      ClusterMap="{x:Null}" 
      IsSideways="False" 
      GlyphOffsets="{x:Null}" 
      GlyphIndices="43 72 79 79 82 3 58 82 85 79 71" 
      BaselineOrigin="0,12.29"  
      FontRenderingEmSize="13.333333333333334" 
      DeviceFontName="{x:Null}" 
      AdvanceWidths="9.62666666666667 7.41333333333333 2.96 2.96 7.41333333333333 3.70666666666667 12.5866666666667 7.41333333333333 4.44 2.96 7.41333333333333" 
      BidiLevel="0">
      <GlyphRun.GlyphTypeface>
        <GlyphTypeface FontUri="C:\WINDOWS\Fonts\TIMES.TTF" />
      </GlyphRun.GlyphTypeface>
    </GlyphRun>
  </GlyphRunDrawing.GlyphRun>
</GlyphRunDrawing>

GlyphRun 是低級別的對象,擬用於固定格式的文檔表示和打印方案。 將文本繪制到屏幕上的一種比較簡單的方法是使用 Label TextBlock 有關 GlyphRun 的更多信息,請參見 GlyphRun 對象和 Glyphs 元素簡介概述。

復合繪圖

使用 DrawingGroup,可將多個繪圖組合為一個復合繪圖。 使用 DrawingGroup,您可將形狀、圖像和文本組合到一個 Drawing 對象中。

下面的示例使用 DrawingGroup 將兩個 GeometryDrawing 對象和一個 ImageDrawing 對象組合到一起。 該示例產生下面的輸出。

復合繪圖


具有多個繪圖的 DrawingGroup

  1.             //
  2.             // Create three drawings.
  3.             //
  4.             GeometryDrawing ellipseDrawing =
  5.                 new GeometryDrawing(
  6.                     new SolidColorBrush(Color.FromArgb(10218124320)),
  7.                     new Pen(Brushes.Black, 4),
  8.                     new EllipseGeometry(new Point(50,50), 5050)
  9.                 );
  10.  
  11.             ImageDrawing kiwiPictureDrawing = 
  12.                 new ImageDrawing(
  13.                     new BitmapImage(new Uri(@"sampleImages\kiwi.png", UriKind.Relative)), 
  14.                     new Rect(50,50,100,100));
  15.  
  16.             GeometryDrawing ellipseDrawing2 =
  17.                 new GeometryDrawing(
  18.                     new SolidColorBrush(Color.FromArgb(102,181,243,20)),
  19.                     new Pen(Brushes.Black, 4),
  20.                     new EllipseGeometry(new Point(150150), 5050)
  21.                 );
  22.  
  23.             // Create a DrawingGroup to contain the drawings.
  24.             DrawingGroup aDrawingGroup = new DrawingGroup();
  25.             aDrawingGroup.Children.Add(ellipseDrawing);
  26.             aDrawingGroup.Children.Add(kiwiPictureDrawing);
  27.             aDrawingGroup.Children.Add(ellipseDrawing2);
  28.  
  29.  

<DrawingGroup>

  <GeometryDrawing Brush="#66B5F314">
    <GeometryDrawing.Geometry>
      <EllipseGeometry Center="50,50" RadiusX="50"  RadiusY="50"/>
    </GeometryDrawing.Geometry>
    <GeometryDrawing.Pen>
      <Pen Brush="Black" Thickness="4" />
    </GeometryDrawing.Pen>
  </GeometryDrawing>
  <ImageDrawing ImageSource="sampleImages\kiwi.png" Rect="50,50,100,100"/>
  <GeometryDrawing Brush="#66B5F314">
    <GeometryDrawing.Geometry>
      <EllipseGeometry Center="150,150" RadiusX="50"  RadiusY="50"/>
    </GeometryDrawing.Geometry>
    <GeometryDrawing.Pen>
      <Pen Brush="Black" Thickness="4" />
    </GeometryDrawing.Pen>
  </GeometryDrawing>
</DrawingGroup>

通過 DrawingGroup,您還可以對其內容應用不透明度蒙版、變換、位圖效果及其他操作。 DrawingGroup 操作按下列順序應用: OpacityMask Opacity BitmapEffect ClipGeometry GuidelineSet Transform

下圖演示 DrawingGroup 操作的應用順序。

DrawingGroup 操作的順序


DrawingGroup 操作順序

下表描述您可用於操作 DrawingGroup 對象內容的屬性。

 

屬性

說明

圖示

OpacityMask

改變 DrawingGroup 內容選定部分的不透明度。 有關示例,請參見 How to: Control the Opacity of a Drawing

具有不透明遮罩的 DrawingGroup

Opacity

統一更改 DrawingGroup 內容的不透明度。 使用此屬性將 Drawing 設置為透明或部分透明。 有關示例,請參見 How to: Apply an Opacity Mask to a Drawing

具有不同不透明度設置的 DrawingGroup

BitmapEffect

BitmapEffect 應用到 DrawingGroup 內容。 有關示例,請參見 How to: Apply a BitmapEffect to a Drawing

具有 BlurBitmapEffect 的 DrawingGroup

ClipGeometry

DrawingGroup 內容剪裁到您使用 Geometry 描述的區域內。 有關示例,請參見 How to: Clip a Drawing

具有定義的剪輯區域的 DrawingGroup

GuidelineSet

根據指定的准則將與設備無關的像素與設備像素對齊。 此屬性有助於確保清晰地在 DPI 較低的顯示器上呈現圖形的細節。 有關示例,請參見 如何:向繪圖應用 GuidelineSet

具有和沒有 GuidelineSet 的 DrawingGroup

Transform

變換 DrawingGroup 內容。 有關示例,請參見 How to: Apply a Transform to a Drawing

旋轉后的 DrawingGroup

 

 

使用 Drawing 繪制對象

DrawingBrush 是一種使用 Drawing 對象繪制區域的畫筆。 您可以使用它來借助 Drawing 繪制任何圖形對象。 DrawingBrush Drawing 屬性描述其 Drawing 若要使用 DrawingBrush 呈現 Drawing,請使用畫筆的 Drawing 屬性將其添加到畫筆中,並使用畫筆繪制圖形對象,例如控件或面板。

下面的示例使用從 GeometryDrawing 創建的圖案通過 DrawingBrush 來繪制 Rectangle Fill 該示例產生下面的輸出。

與 DrawingBrush 一起使用的 GeometryDrawing

平鋪的 DrawingBrush
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. using System.Windows.Media.Animation;
  6. using System.Windows.Shapes;
  7.  
  8. namespace SDKSample
  9. {
  10.     public class DrawingBrushExample : Page
  11.     {
  12.  
  13.         public DrawingBrushExample()
  14.         {
  15.  
  16.             //
  17.             // Create the Geometry to draw.
  18.             //
  19.             GeometryGroup ellipses = new GeometryGroup();
  20.             ellipses.Children.Add(
  21.                 new EllipseGeometry(new Point(50,50), 4520)
  22.                 );
  23.             ellipses.Children.Add(
  24.                 new EllipseGeometry(new Point(5050), 2045)
  25.                 );
  26.  
  27.             //
  28.             // Create a GeometryDrawing.
  29.             //
  30.             GeometryDrawing aGeometryDrawing = new GeometryDrawing();
  31.             aGeometryDrawing.Geometry = ellipses;
  32.  
  33.             // Paint the drawing with a gradient.
  34.             aGeometryDrawing.Brush = 
  35.                 new LinearGradientBrush(
  36.                     Colors.Blue, 
  37.                     Color.FromRgb(204,204,255), 
  38.                     new Point(0,0), 
  39.                     new Point(1,1));
  40.  
  41.             // Outline the drawing with a solid color.
  42.             aGeometryDrawing.Pen = new Pen(Brushes.Black, 10);
  43.  
  44.             DrawingBrush patternBrush = new DrawingBrush(aGeometryDrawing);
  45.             patternBrush.Viewport = new Rect(000.250.25);
  46.             patternBrush.TileMode = TileMode.Tile;
  47.             patternBrush.Freeze();
  48.  
  49.             //
  50.             // Create an object to paint.
  51.             //
  52.             Rectangle paintedRectangle = new Rectangle();
  53.             paintedRectangle.Width = 100;
  54.             paintedRectangle.Height = 100;
  55.             paintedRectangle.Fill = patternBrush;
  56.  
  57.             //
  58.             // Place the image inside a border and
  59.             // add it to the page.
  60.             //
  61.             Border exampleBorder = new Border();
  62.             exampleBorder.Child = paintedRectangle;
  63.             exampleBorder.BorderBrush = Brushes.Gray;
  64.             exampleBorder.BorderThickness = new Thickness(1);
  65.             exampleBorder.HorizontalAlignment = HorizontalAlignment.Left;
  66.             exampleBorder.VerticalAlignment = VerticalAlignment.Top;
  67.             exampleBorder.Margin = new Thickness(10);
  68.  
  69.             this.Margin = new Thickness(20);
  70.             this.Background = Brushes.White;
  71.             this.Content = exampleBorder;
  72.         }
  73.     }
  74. }
  75.  

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

  <Border BorderBrush="Gray" BorderThickness="1" 
    HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="10">
    <Rectangle Width="100" Height="100">
      <Rectangle.Fill>
        <DrawingBrush PresentationOptions:Freeze="True"
                      Viewport="0,0,0.25,0.25" TileMode="Tile">
          <DrawingBrush.Drawing>
            <GeometryDrawing>
              <GeometryDrawing.Geometry>
                <GeometryGroup>
                  <EllipseGeometry Center="50,50" RadiusX="45" RadiusY="20" />
                  <EllipseGeometry Center="50,50" RadiusX="20" RadiusY="45" />
                </GeometryGroup>
              </GeometryDrawing.Geometry>
              <GeometryDrawing.Brush>
                <LinearGradientBrush>
                  <GradientStop Offset="0.0" Color="Blue" />
                  <GradientStop Offset="1.0" Color="#CCCCFF" />
                </LinearGradientBrush>
              </GeometryDrawing.Brush>
              <GeometryDrawing.Pen>
                <Pen Thickness="10" Brush="Black" />
              </GeometryDrawing.Pen>
            </GeometryDrawing>
          </DrawingBrush.Drawing>
        </DrawingBrush>
      </Rectangle.Fill>

    </Rectangle>
  </Border>


</Page>

DrawingBrush 類提供了用於拉伸和平鋪其內容的各種選項。 有關 DrawingBrush 的更多信息,請參見 使用圖像、繪圖和 Visual 進行繪制概述。

參考

概念

http://technet.microsoft.com/zh-cn/office/ms751619%28v=vs.110%29


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM