下面列出了不同类型的 Drawing 对象。
Drawing 对象。
-
Image 控件将其显示为图像。
-
Background。
-
DrawingVisual 的外观。
-
Visual 的内容。
Visual 对象进行低级绘制的理想选择。
Freezable 对象概述。
绘制形状
Pen 属性描述其轮廓的绘制方式。
Pen 进行绘制。
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 概述。
WPF 中的形状和基本绘图概述。
绘制图像
Rect 属性定义绘制图像的区域。
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"/>
图像处理概述。
播放媒体(仅限代码)
说明
|
|
MediaElement。
|
VideoDrawing 一起使用。
说明
|
|
Always。
|
MediaTimeline 的情况下播放媒体,请执行下列步骤。
-
MediaPlayer 对象。
- MediaPlayer player = new MediaPlayer();
-
-
Open 方法加载媒体文件。
- player.Open(new Uri(@"sampleMedia\xbox.wmv", UriKind.Relative));
-
-
VideoDrawing。
- VideoDrawing aVideoDrawing = new VideoDrawing();
-
-
Rect 属性指定要绘制媒体的大小和位置。
- aVideoDrawing.Rect = new Rect(0, 0, 100, 100);
-
-
Player 属性。
- aVideoDrawing.Player = player;
-
-
Play 方法开始播放媒体。
- // Play the video once.
- player.Play();
-
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();
-
-
VideoDrawing 一起使用,请执行下列步骤:
-
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。
- // Create a clock from the MediaTimeline.
- MediaClock mClock = mTimeline.CreateClock();
-
-
Clock 属性。
- MediaPlayer repeatingVideoDrawingPlayer = new MediaPlayer();
- repeatingVideoDrawingPlayer.Clock = mClock;
-
-
Player 属性。
- VideoDrawing repeatingVideoDrawing = new VideoDrawing();
- repeatingVideoDrawing.Rect = new Rect(150, 0, 100, 100);
- repeatingVideoDrawing.Player = repeatingVideoDrawingPlayer;
-
VideoDrawing 来反复播放某视频。
- //
- // 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;
-
MediaPlayer 的交互式方法。
绘制文本
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 对象和 Glyphs 元素简介概述。
复合绘图
Drawing 对象中。
该示例产生下面的输出。
复合绘图

- //
- // 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>
Transform。
DrawingGroup 操作的应用顺序。
DrawingGroup 操作的顺序

下表描述您可用于操作
DrawingGroup 对象内容的属性。
使用 Drawing 绘制对象
Drawing 属性将其添加到画笔中,并使用画笔绘制图形对象,例如控件或面板。
该示例产生下面的输出。
与 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>
使用图像、绘图和 Visual 进行绘制概述。
参考
概念
http://technet.microsoft.com/zh-cn/office/ms751619%28v=vs.110%29
相关文章: