WPF中的Drawing
以前在用WinForm的时候,可以通过GDI+接口在窗体上动态绘制自定义的图形。在WPF中有没有对应的API呢,最近项目中用到了这个,在这里总结一下。
WPF中的Drawing主要提供了几类API:
1. Drawing类型

该组类型主要用来对绘制的对象的描述。比如GeometryDrawing是描述一个几何图形的Drawing,它的Geometry属性定义了它所描述的几何图形是什么样子。(它可以是如下Geometry类型的派生类型的任何一种,GeometryGroup是对多个Geometry的组合)

与GeometryGroup一样,DrawingGroup是对多个Drawing的组合。
2. DrawingContext类型
Drawing类型是以对象的形式来描述要绘制的图形,文字或视频。DrawingContext是通过存储绘制命令的形式来对要绘制的对象进行描述。这种方式更加简单。我们可以通过DrawingGroup.Open方法来得到一个DrawingContext,然后可以调用如下接口来进行操作。

探其究竟,其实DrawingContext仅仅是将对Drawing对象的创建与管理进行了包装,然后提供了一组比较方便使用的接口而已。
看反编译出来的代码:
public override void DrawEllipse(Brush brush, Pen pen, Point center, AnimationClock centerAnimations, double radiusX, AnimationClock radiusXAnimations, double radiusY, AnimationClock radiusYAnimations)
{
this.VerifyApiNonstructuralChange();
if ((brush != null) || (pen != null))
{
EllipseGeometry newFreezable = new EllipseGeometry(center, radiusX, radiusY) {
CanBeInheritanceContext = this.CanBeInheritanceContext
};
this.SetupNewFreezable(newFreezable, ((centerAnimations == null) && (radiusXAnimations == null)) && (radiusYAnimations == null));
if (centerAnimations != null)
{
newFreezable.ApplyAnimationClock(EllipseGeometry.CenterProperty, centerAnimations);
}
if (radiusXAnimations != null)
{
newFreezable.ApplyAnimationClock(EllipseGeometry.RadiusXProperty, radiusXAnimations);
}
if (radiusYAnimations != null)
{
newFreezable.ApplyAnimationClock(EllipseGeometry.RadiusYProperty, radiusYAnimations);
}
this.AddNewGeometryDrawing(brush, pen, newFreezable);
}
} private void AddNewGeometryDrawing(Brush brush, Pen pen, Geometry geometry)
{
GeometryDrawing newFreezable = new GeometryDrawing {
CanBeInheritanceContext = this.CanBeInheritanceContext,
Brush = brush,
Pen = pen,
Geometry = geometry
};
this.SetupNewFreezable(newFreezable, (((brush == null) || brush.IsFrozen) && ((pen == null) || pen.IsFrozen)) && geometry.IsFrozen);
this.AddDrawing(newFreezable);
} private void AddDrawing(Drawing newDrawing)
{
if (this._rootDrawing == null)
{
this._rootDrawing = newDrawing;
}
else if (this._currentDrawingGroup == null)
{
this._currentDrawingGroup = new DrawingGroup();
this._currentDrawingGroup.CanBeInheritanceContext = this.CanBeInheritanceContext;
this.SetupNewFreezable(this._currentDrawingGroup, false);
this._currentDrawingGroup.Children.Add(this._rootDrawing);
this._currentDrawingGroup.Children.Add(newDrawing);
this._rootDrawing = this._currentDrawingGroup;
}
else
{
this._currentDrawingGroup.Children.Add(newDrawing);
}
}
3.接受Drawing作为Content的类型
可以通过这些类型来应用Drawing中描述的对象绘制。
主要有如下几种:
(1)DrawingImage,结合Image控件可以将Drawing显示在UI上

(2) DrawingBrush, 都有了Brush了,那就可以在仍何控件画了。比如说作为控件的Background.

(3) DrawingVisual类型

通过该类型,可以将Drawing创建为一个Visual.然后可以将Visual提供给一VisualContainer(可以自定义从FrameworkElement派生的类型,然后重写GetVisualChild与VisualChildrenCount方法作为VisualContainer)作为其UI呈现。
结合RenderTargetBitmap和BitmapEncoder, 我们可以将Drawing呈现到Bitmap图片文件中。

WPF中的Drawing的更多相关文章
- WPF中找不到Image或者Image不是Drawing系列
WPF中默认没有引用WinForm里面的一些东西,都是用它自带的那一套,但又不能完全脱离,所以有的时候比较蛋疼
- WPF中的image控件的Source赋值
WPF中的Image控件Source的设置 1.XAML中 简单的方式(Source="haha.png"); image控件的Source设置为相对路径后(Source=&quo ...
- WPF中实现验证码功能
其实和winform中的实现差不多,只是由于WPF中控件使用的库与winform中的有区别,大体上还是差不多的,直接看代码: 产生验证码的类:ValidCode.cs public class Val ...
- GMap.Net开发之在WinForm和WPF中使用GMap.Net地图插件
GMap.NET是什么? 来看看它的官方说明:GMap.NET is great and Powerful, Free, cross platform, open source .NET contro ...
- [转]在WPF中使用WinForm控件方法
本文转自:http://blog.csdn.net/lianchangshuai/article/details/6415241 下面以在Wpf中添加ZedGraph(用于创建任意数据的二维线型.条型 ...
- 在WPF中使用AForge.net控制摄像头拍照
原文:在WPF中使用AForge.net控制摄像头拍照 利用AForge.net控制摄像头拍照最方便的方法就是利用PictureBox显示摄像头画面,但在WPF中不能直接使用PictureBox.必须 ...
- 在WPF中自定义你的绘制(五)
原文:在WPF中自定义你的绘制(五) 在WPF中自定义你的绘制(五) ...
- 在WPF中自定义你的绘制(二)
原文:在WPF中自定义你的绘制(二) 在WPF中自定义你的绘制(二) ...
- 浏览器扩展系列————在WPF中定制WebBrowser快捷菜单
原文:浏览器扩展系列----在WPF中定制WebBrowser快捷菜单 关于如何定制菜单可以参考codeproject上的这篇文章:http://www.codeproject.com/KB/book ...
随机推荐
- (转)c指针
转自:http://www.cnblogs.com/wchhuangya/archive/2009/12/24/1631121.html 这两天开始搞BREW了,用的是C的语法.上学时学过的C都还给学 ...
- UML类图总结
前言 类图和序列图是UML中最常用的两种Diagram.我将做详细的总结.在许多书中,或者网站中,在介绍一个系统的子系统的设计时,很多时候,都是给出简单的类图来简述构成子系统的类之间的关系.这足以说明 ...
- spicy及remote-viewer登录方法
spicy登录: $sudo spicy remote-viewer登录: $ sudo /usr/local/bin/remote-viewer $ spice://192.168.70.158:4 ...
- 深入浅出C++引用(Reference)类型
要点1:为反复使用的.冗长的变量名称定义一个简短的.易用的别名,从而简化了代码.通常,冗长的变量名称源于多层嵌套对象,例如类中定义嵌套类,类中定义其它类对象. //------ 未使用引用的程序片段, ...
- 【Leetcode】 - Divide Two Integers 位运算实现整数除法
实现两个整数的除法,不许用乘法.除法和求模.题目被贴上了BinarySearch,但我没理解为什么会和BinarySearch有关系.我想的方法也和BS一点关系都没有. 很早以前我就猜想,整数的乘法是 ...
- java 回传参数
通过 new 创建的对象可以实现回传,如数组:自定义类对象里的参数. [数组方式] public static void main(String[] args) { try { int [] amou ...
- git/github在windows上使用
问题描述: git在Windows上的使用 问题解决: (1)下载安装git http://msysgit.github.io/ 到该网址中下载msgit软件 注: 安装msg ...
- 有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
先写我的思路,没有用指针的做法.如果你用的是VC,把第六行去掉. #include<stdio.h> #include<stdlib.h> int main() { setvb ...
- Searching a 2D Sorted Matrix Part I
Write an efficient algorithm that searches for a value in an n x m table (two-dimensional array). Th ...
- VBScript 函数
Date/Time 函数 Conversion 函数 Format 函数 Math 函数 Array 函数 String 函数 其他函数 Date/Time 函数 函数 描述 CDate 把一个有效的 ...