以前在用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派生的类型,然后重写GetVisualChildVisualChildrenCount方法作为VisualContainer)作为其UI呈现。

结合RenderTargetBitmap和BitmapEncoder, 我们可以将Drawing呈现到Bitmap图片文件中。

WPF中的Drawing的更多相关文章

  1. WPF中找不到Image或者Image不是Drawing系列

    WPF中默认没有引用WinForm里面的一些东西,都是用它自带的那一套,但又不能完全脱离,所以有的时候比较蛋疼

  2. WPF中的image控件的Source赋值

    WPF中的Image控件Source的设置 1.XAML中 简单的方式(Source="haha.png"); image控件的Source设置为相对路径后(Source=&quo ...

  3. WPF中实现验证码功能

    其实和winform中的实现差不多,只是由于WPF中控件使用的库与winform中的有区别,大体上还是差不多的,直接看代码: 产生验证码的类:ValidCode.cs public class Val ...

  4. GMap.Net开发之在WinForm和WPF中使用GMap.Net地图插件

    GMap.NET是什么? 来看看它的官方说明:GMap.NET is great and Powerful, Free, cross platform, open source .NET contro ...

  5. [转]在WPF中使用WinForm控件方法

    本文转自:http://blog.csdn.net/lianchangshuai/article/details/6415241 下面以在Wpf中添加ZedGraph(用于创建任意数据的二维线型.条型 ...

  6. 在WPF中使用AForge.net控制摄像头拍照

    原文:在WPF中使用AForge.net控制摄像头拍照 利用AForge.net控制摄像头拍照最方便的方法就是利用PictureBox显示摄像头画面,但在WPF中不能直接使用PictureBox.必须 ...

  7. 在WPF中自定义你的绘制(五)

    原文:在WPF中自定义你的绘制(五) 在WPF中自定义你的绘制(五)                                                                   ...

  8. 在WPF中自定义你的绘制(二)

    原文:在WPF中自定义你的绘制(二)   在WPF中自定义你的绘制(二)                                                                 ...

  9. 浏览器扩展系列————在WPF中定制WebBrowser快捷菜单

    原文:浏览器扩展系列----在WPF中定制WebBrowser快捷菜单 关于如何定制菜单可以参考codeproject上的这篇文章:http://www.codeproject.com/KB/book ...

随机推荐

  1. cocos2dx中的菜单项CCMenuItem及其五个子类的使用

    /*CCMenuItem是一个虚基类,因此必须实现它的五个子类之一,再把子类对象赋给父类指针,相当于多态*/ CCMenuItem *fontItem = CCMenuItemFont::create ...

  2. Ubuntu重启网络/etc/init.d/networking restart报错

    Linux版本:Ubuntu 12.04 配置网口后重启网络,提示/etc/init.d/networking restart is deprecated. $ sudo /etc/init.d/ne ...

  3. html+css学习笔记 5[表格、表单]

    表格 -- 默认样式重置 表格标签:     table 表格     thead 表格头     tbody 表格主体     tfoot 表格尾     tr 表格行     th 元素定义表头 ...

  4. 常用的Eclilpse插件列表以及安装方式总结

    Eclipse常用插件的安装方式总结: 1.Maven Integration for Eclipse WTP     作用:用来方便开发和使用maven项目.     安装方式:Eclipse Ma ...

  5. 设置HTTP header方式

    一, Server Code JSP----> <%@ page language="java" contentType="text/html; charse ...

  6. [转载]Spring Bean Configuration Inheritance

    转自: http://www.mkyong.com/spring/spring-bean-configuration-inheritance/ In Spring, the inheritance i ...

  7. javascript加速运动

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. hdu 1709 The Balance

    母函数的特殊情况,左右两边都可以放,如样例1,2,9 母函数为(1+x+1/x)*(1+x^2+1/x^2)*(1+x^9+1/x^9) 化简为(1+x+x^2)*(1+x^2+x^4)*(1+x^9 ...

  9. YWE复习

    1.怎样使用C#的[状态栏][StatusStrip]控件 http://jingyan.baidu.com/article/851fbc37e7004e3e1f15ab3d.html 2.toolt ...

  10. 关于C#中timer类

    ·关于C#中timer类 在C#里关于定时器类就有3个 1.定义在System.Windows.Forms里 2.定义在System.Threading.Timer类里 3.定义在System.Tim ...