繪圖物件概觀
本主題介紹 Drawing 物件,並說明如何使用物件來有效率地繪製圖形、點陣圖、文字和媒體。 當您建立美工圖案、使用 繪製或 DrawingBrush 使用 物件時,請使用 VisualDrawing 物件。
什麼是繪圖物件
Drawing物件描述可見的內容,例如圖形、點陣圖、視訊或文字行。 不同類型的繪圖可描繪不同類型的內容。 以下列出不同類型的繪圖物件。
GeometryDrawing – 繪製圖形。
ImageDrawing – 繪製影像。
GlyphRunDrawing – 繪製文字。
VideoDrawing – 播放音訊或視訊檔案。
DrawingGroup – 繪製其他繪圖。 您可以使用繪圖群組,將其他繪圖結合為單一複合繪圖。
Drawing 物件多用途;有許多方式可以使用 Drawing 物件。
您可以使用 和 Image 控制項,將它顯示為影像 DrawingImage 。
您可以使用它搭配 DrawingBrush 繪製 物件,例如 Background 的 Page 。
您可以使用它來描述 的外觀 DrawingVisual 。
您可以使用它來列舉 的內容 Visual 。
WPF 提供其他能夠繪製圖形、點陣圖、文字及媒體的物件類型。 例如,您也可以使用 Shape 物件來繪製圖形,而 MediaElement 控制項會提供另一種方式,將視訊新增至您的應用程式。 那麼,何時應該使用 Drawing 物件? 當您可以犧牲架構層級功能來獲得效能優勢或需要 Freezable 功能時。 因為 Drawing 物件缺少配置 、輸入和焦點的支援,因此它們可提供效能優點,讓它們非常適合用來描述背景、美工圖案,以及使用 Visual 物件進行低階繪圖。
因為它們是類型 Freezable 物件, Drawing 因此物件會取得數個特殊功能,包括下列各項:它們可以宣告為 資源 、在多個物件之間共用、進行唯讀來改善效能、複製,以及讓執行緒安全。 如需物件所提供 Freezable 之不同功能的詳細資訊,請參閱 Freezable 物件概觀 。
繪製圖形
若要繪製圖形,您可以使用 GeometryDrawing 。 幾何繪圖的 Geometry 屬性描述要繪製的圖形、其 Brush 屬性描述圖形的內部應該如何繪製,而其 Pen 屬性描述其外框的繪製方式。
下列範例會使用 GeometryDrawing 繪製圖形。 圖形是由 和 兩 EllipseGeometry 個 GeometryGroup 物件所描述。 圖形的內部會使用 LinearGradientBrush 繪製,而且其外框會以 BlackPen 繪製。
此範例會建立下列 GeometryDrawing 。
GeometryDrawing
//
// Create the Geometry to draw.
//
GeometryGroup ellipses = new GeometryGroup();
ellipses.Children.Add(
new EllipseGeometry(new Point(50,50), 45, 20)
);
ellipses.Children.Add(
new EllipseGeometry(new Point(50, 50), 20, 45)
);
//
// Create a GeometryDrawing.
//
GeometryDrawing aGeometryDrawing = new GeometryDrawing();
aGeometryDrawing.Geometry = ellipses;
// Paint the drawing with a gradient.
aGeometryDrawing.Brush =
new LinearGradientBrush(
Colors.Blue,
Color.FromRgb(204,204,255),
new Point(0,0),
new Point(1,1));
// Outline the drawing with a solid color.
aGeometryDrawing.Pen = new Pen(Brushes.Black, 10);
<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
// Create a 100 by 100 image with an upper-left point of (75,75).
ImageDrawing bigKiwi = new ImageDrawing();
bigKiwi.Rect = new Rect(75, 75, 100, 100);
bigKiwi.ImageSource = new BitmapImage(
new Uri(@"sampleImages\kiwi.png", UriKind.Relative));
<!-- 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"/>
如需有關影像的詳細資訊,請參閱影像處理概觀。
播放媒體 (僅程式碼)
注意
雖然您可以在 Extensible Application Markup Language (XAML) 中宣告 VideoDrawing ,但您只能使用程式碼載入和播放其媒體。 若要在 Extensible Application Markup Language (XAML) 中播放影片,請改用 MediaElement 。
若要播放音訊或視訊檔案,您可以使用 VideoDrawing 和 MediaPlayer 。 有兩種方法可以載入及播放媒體。 第一個是單獨使用 MediaPlayer 和 VideoDrawing ,第二種方式是建立您自己的 MediaTimeline 來搭配 MediaPlayer 和 VideoDrawing 使用。
注意
利用您的應用程式散發媒體時,您無法跟散發影像一樣,使用媒體檔案當作專案資源。 在您的專案檔中,您必須改為將媒體類型設定為 Content
,並將 CopyToOutputDirectory
設定為 PreserveNewest
或 Always
。
若要播放媒體而不建立您自己的 MediaTimeline ,請執行下列步驟。
建立 MediaPlayer 物件。
MediaPlayer player = new MediaPlayer();
Open使用 方法來載入媒體檔案。
player.Open(new Uri(@"sampleMedia\xbox.wmv", UriKind.Relative));
建立 VideoDrawing。
VideoDrawing aVideoDrawing = new VideoDrawing();
設定 的 VideoDrawing 屬性,以指定要繪製媒體 Rect 的大小和位置。
aVideoDrawing.Rect = new Rect(0, 0, 100, 100);
使用您建立的 來 Player 設定 的 MediaPlayerVideoDrawing 屬性。
aVideoDrawing.Player = player;
Play使用 的 MediaPlayer 方法來開始播放媒體。
// Play the video once. player.Play();
下列範例會使用 VideoDrawing 和 MediaPlayer 來播放視訊檔案一次。
//
// Create a VideoDrawing.
//
MediaPlayer player = new MediaPlayer();
player.Open(new Uri(@"sampleMedia\xbox.wmv", UriKind.Relative));
VideoDrawing aVideoDrawing = new VideoDrawing();
aVideoDrawing.Rect = new Rect(0, 0, 100, 100);
aVideoDrawing.Player = player;
// Play the video once.
player.Play();
若要取得媒體的其他計時控制,請搭配 MediaPlayer 和 VideoDrawing 物件使用 MediaTimeline 。 MediaTimeline可讓您指定影片是否應該重複。 若要搭配 VideoDrawing 使用 MediaTimeline ,請執行下列步驟:
MediaTimeline宣告 並設定其計時行為。
// Create a MediaTimeline. MediaTimeline mTimeline = new MediaTimeline(new Uri(@"sampleMedia\xbox.wmv", UriKind.Relative)); // Set the timeline to repeat. mTimeline.RepeatBehavior = RepeatBehavior.Forever;
MediaClock從 MediaTimeline 建立 。
// Create a clock from the MediaTimeline. MediaClock mClock = mTimeline.CreateClock();
MediaPlayer建立 並使用 MediaClock 來設定其 Clock 屬性。
MediaPlayer repeatingVideoDrawingPlayer = new MediaPlayer(); repeatingVideoDrawingPlayer.Clock = mClock;
建立 , VideoDrawing 並將 指派 MediaPlayer 給 Player 的 VideoDrawing 屬性。
VideoDrawing repeatingVideoDrawing = new VideoDrawing(); repeatingVideoDrawing.Rect = new Rect(150, 0, 100, 100); repeatingVideoDrawing.Player = repeatingVideoDrawingPlayer;
下列範例使用 MediaTimeline 搭配 和 MediaPlayerVideoDrawing 來重複播放視訊。
//
// Create a VideoDrawing that repeats.
//
// Create a MediaTimeline.
MediaTimeline mTimeline =
new MediaTimeline(new Uri(@"sampleMedia\xbox.wmv", UriKind.Relative));
// Set the timeline to repeat.
mTimeline.RepeatBehavior = RepeatBehavior.Forever;
// Create a clock from the MediaTimeline.
MediaClock mClock = mTimeline.CreateClock();
MediaPlayer repeatingVideoDrawingPlayer = new MediaPlayer();
repeatingVideoDrawingPlayer.Clock = mClock;
VideoDrawing repeatingVideoDrawing = new VideoDrawing();
repeatingVideoDrawing.Rect = new Rect(150, 0, 100, 100);
repeatingVideoDrawing.Player = repeatingVideoDrawingPlayer;
請注意,當您使用 時,會使用 MediaTimeline 從 Controller 屬性傳回的 MediaClock 互動式 ClockController ,以控制媒體播放,而不是 的 MediaPlayer 互動式方法。
繪製文字
若要繪製文字,您可以使用 GlyphRunDrawing 和 GlyphRun 。 下列範例使用 GlyphRunDrawing 來繪製 「Hello World」 文字。
GlyphRun theGlyphRun = new GlyphRun(
new GlyphTypeface(new Uri(@"C:\WINDOWS\Fonts\TIMES.TTF")),
0,
false,
13.333333333333334,
new ushort[]{43, 72, 79, 79, 82, 3, 58, 82, 85, 79, 71},
new Point(0, 12.29),
new double[]{
9.62666666666667, 7.41333333333333, 2.96,
2.96, 7.41333333333333, 3.70666666666667,
12.5866666666667, 7.41333333333333,
4.44, 2.96, 7.41333333333333},
null,
null,
null,
null,
null,
null
);
GlyphRunDrawing gDrawing = new GlyphRunDrawing(Brushes.Black, theGlyphRun);
<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 。 此範例會產生下列輸出。
複合圖形
//
// Create three drawings.
//
GeometryDrawing ellipseDrawing =
new GeometryDrawing(
new SolidColorBrush(Color.FromArgb(102, 181, 243, 20)),
new Pen(Brushes.Black, 4),
new EllipseGeometry(new Point(50,50), 50, 50)
);
ImageDrawing kiwiPictureDrawing =
new ImageDrawing(
new BitmapImage(new Uri(@"sampleImages\kiwi.png", UriKind.Relative)),
new Rect(50,50,100,100));
GeometryDrawing ellipseDrawing2 =
new GeometryDrawing(
new SolidColorBrush(Color.FromArgb(102,181,243,20)),
new Pen(Brushes.Black, 4),
new EllipseGeometry(new Point(150, 150), 50, 50)
);
// Create a DrawingGroup to contain the drawings.
DrawingGroup aDrawingGroup = new DrawingGroup();
aDrawingGroup.Children.Add(ellipseDrawing);
aDrawingGroup.Children.Add(kiwiPictureDrawing);
aDrawingGroup.Children.Add(ellipseDrawing2);
<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 、、 BitmapEffectOpacity 、 ClipGeometry 、 GuidelineSet 、 和 。 Transform
下圖顯示套用作業的順序 DrawingGroup 。
DrawingGroup 作業的順序
下表描述可用來操作 DrawingGroup 物件內容的屬性。
屬性 | 說明 | 說明 |
---|---|---|
OpacityMask | 改變內容選取部分的 DrawingGroup 不透明度。 如需範例,請參閱操作說明︰控制繪圖的不透明度。 | |
Opacity | 統一變更內容的不透明度 DrawingGroup 。 使用這個屬性讓透明 Drawing 或部分透明。 如需範例,請參閱操作說明︰對繪圖套用不透明度遮罩。 | |
BitmapEffect | 將 BitmapEffect 套用至 DrawingGroup 內容。 如需範例,請參閱操作說明︰對繪圖套用 BitmapEffect。 | |
ClipGeometry | 將 DrawingGroup 內容裁剪至您使用 Geometry 所描述的區域。 如需範例,請參閱操作說明︰裁剪繪圖。 | |
GuidelineSet | 沿著指定的標線將裝置獨立像素貼齊裝置像素。 這個屬性可用於確保在低 DPI 顯示器上呈現清晰的細緻圖形。 如需範例,請參閱對繪圖套用 GuidelineSet。 | |
Transform | DrawingGroup轉換內容。 如需範例,請參閱操作說明︰對繪圖套用轉換。 |
將繪圖顯示為影像
若要使用 Image 控制項來顯示 Drawing ,請使用 DrawingImage 做為 Image 控制項的 SourceDrawingImage.Drawing ,並將 物件的 屬性設定 DrawingImage 為您想要顯示的繪圖。
下列範例使用 DrawingImage 和 Image 控制項來顯示 GeometryDrawing 。 此範例會產生下列輸出。
DrawingImage
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SDKSample
{
public class DrawingImageExample : Page
{
public DrawingImageExample()
{
//
// Create the Geometry to draw.
//
GeometryGroup ellipses = new GeometryGroup();
ellipses.Children.Add(
new EllipseGeometry(new Point(50,50), 45, 20)
);
ellipses.Children.Add(
new EllipseGeometry(new Point(50, 50), 20, 45)
);
//
// Create a GeometryDrawing.
//
GeometryDrawing aGeometryDrawing = new GeometryDrawing();
aGeometryDrawing.Geometry = ellipses;
// Paint the drawing with a gradient.
aGeometryDrawing.Brush =
new LinearGradientBrush(
Colors.Blue,
Color.FromRgb(204,204,255),
new Point(0,0),
new Point(1,1));
// Outline the drawing with a solid color.
aGeometryDrawing.Pen = new Pen(Brushes.Black, 10);
//
// Use a DrawingImage and an Image control
// to display the drawing.
//
DrawingImage geometryImage = new DrawingImage(aGeometryDrawing);
// Freeze the DrawingImage for performance benefits.
geometryImage.Freeze();
Image anImage = new Image();
anImage.Source = geometryImage;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
//
// Place the image inside a border and
// add it to the page.
//
Border exampleBorder = new Border();
exampleBorder.Child = anImage;
exampleBorder.BorderBrush = Brushes.Gray;
exampleBorder.BorderThickness = new Thickness(1);
exampleBorder.HorizontalAlignment = HorizontalAlignment.Left;
exampleBorder.VerticalAlignment = VerticalAlignment.Top;
exampleBorder.Margin = new Thickness(10);
this.Margin = new Thickness(20);
this.Background = Brushes.White;
this.Content = exampleBorder;
}
}
}
<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"
Background="White" Margin="20">
<Border BorderBrush="Gray" BorderThickness="1"
HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="10">
<!-- This image uses a Drawing object for its source. -->
<Image>
<Image.Source>
<DrawingImage PresentationOptions:Freeze="True">
<DrawingImage.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>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>
</Border>
</Page>
使用繪圖繪製物件
DrawingBrush是一種筆刷類型,可繪製繪圖物件的區域。 您可以使用它來繪製含有繪圖的任何圖形物件。 Drawing的 DrawingBrush 屬性描述其 Drawing 。 若要使用 DrawingBrush 呈現 Drawing ,請使用筆刷的 Drawing 屬性將它新增至筆刷,並使用筆刷繪製繪圖物件,例如控制項或面板。
下列範例會使用 DrawingBrush 繪製 Fill 的 ,其具有從 GeometryDrawing 建立的 Rectangle 模式。 此範例會產生下列輸出。
搭配 DrawingBrush 使用的 GeometryDrawing
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SDKSample
{
public class DrawingBrushExample : Page
{
public DrawingBrushExample()
{
//
// Create the Geometry to draw.
//
GeometryGroup ellipses = new GeometryGroup();
ellipses.Children.Add(
new EllipseGeometry(new Point(50,50), 45, 20)
);
ellipses.Children.Add(
new EllipseGeometry(new Point(50, 50), 20, 45)
);
//
// Create a GeometryDrawing.
//
GeometryDrawing aGeometryDrawing = new GeometryDrawing();
aGeometryDrawing.Geometry = ellipses;
// Paint the drawing with a gradient.
aGeometryDrawing.Brush =
new LinearGradientBrush(
Colors.Blue,
Color.FromRgb(204,204,255),
new Point(0,0),
new Point(1,1));
// Outline the drawing with a solid color.
aGeometryDrawing.Pen = new Pen(Brushes.Black, 10);
DrawingBrush patternBrush = new DrawingBrush(aGeometryDrawing);
patternBrush.Viewport = new Rect(0, 0, 0.25, 0.25);
patternBrush.TileMode = TileMode.Tile;
patternBrush.Freeze();
//
// Create an object to paint.
//
Rectangle paintedRectangle = new Rectangle();
paintedRectangle.Width = 100;
paintedRectangle.Height = 100;
paintedRectangle.Fill = patternBrush;
//
// Place the image inside a border and
// add it to the page.
//
Border exampleBorder = new Border();
exampleBorder.Child = paintedRectangle;
exampleBorder.BorderBrush = Brushes.Gray;
exampleBorder.BorderThickness = new Thickness(1);
exampleBorder.HorizontalAlignment = HorizontalAlignment.Left;
exampleBorder.VerticalAlignment = VerticalAlignment.Top;
exampleBorder.Margin = new Thickness(10);
this.Margin = new Thickness(20);
this.Background = Brushes.White;
this.Content = exampleBorder;
}
}
}
<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 ,請參閱 使用影像、繪圖和視覺效果小畫家概 觀。
使用視覺物件呈現繪圖
DrawingVisual是設計來呈現繪圖的視覺物件類型。 開發人員若想要建置高度自訂的圖形化環境,可以選擇直接在視覺分層處理,但這不屬於本概觀的說明範圍。 如需詳細資訊,請參閱使用 DrawingVisual 物件概觀。
DrawingContext 物件
類別 DrawingContext 可讓您使用視覺內容填入 Visual 或 Drawing 。 許多這類較低層級的繪圖物件會使用 , DrawingContext 因為它非常有效率地描述圖形內容。
DrawingContext雖然繪製方法看起來與型別的 System.Drawing.Graphics 繪製方法類似,但它們實際上非常不同。 DrawingContext 與保留模式圖形系統搭配使用,而 System.Drawing.Graphics 類型則與即時模式圖形系統搭配使用。 當您使用 DrawingContext 物件的繪製命令時,實際上會儲存一組轉譯指令(雖然確切的儲存機制取決於圖形系統稍後將使用的物件類型 DrawingContext ),但您不會即時繪製到螢幕。 如需 Windows Presentation Foundation (WPF) 圖形系統運作方式的詳細資訊,請參閱 WPF 圖形轉譯概觀 。
您永遠不會直接具現化 DrawingContext ;不過,您可以從某些方法取得繪圖內容,例如 DrawingGroup.Open 和 DrawingVisual.RenderOpen 。
列舉視覺物件的內容
除了其他用途之外, Drawing 物件也會提供物件模型來列舉 的內容 Visual 。
下列範例會 GetDrawing 使用 方法來擷 DrawingGroup 取 的值 Visual 並加以列舉。
public void RetrieveDrawing(Visual v)
{
DrawingGroup drawingGroup = VisualTreeHelper.GetDrawing(v);
EnumDrawingGroup(drawingGroup);
}
// Enumerate the drawings in the DrawingGroup.
public void EnumDrawingGroup(DrawingGroup drawingGroup)
{
DrawingCollection dc = drawingGroup.Children;
// Enumerate the drawings in the DrawingCollection.
foreach (Drawing drawing in dc)
{
// If the drawing is a DrawingGroup, call the function recursively.
if (drawing is DrawingGroup group)
{
EnumDrawingGroup(group);
}
else if (drawing is GeometryDrawing)
{
// Perform action based on drawing type.
}
else if (drawing is ImageDrawing)
{
// Perform action based on drawing type.
}
else if (drawing is GlyphRunDrawing)
{
// Perform action based on drawing type.
}
else if (drawing is VideoDrawing)
{
// Perform action based on drawing type.
}
}
}
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應