首先,由于WPF中不象GDI+中有Graphics对象,因此你无法使用Graphics进行绘图了,取而代之的是:DrawingContext;类似地,GDI+中的OnPaint已被OnRender取代。其次,UIElement有一个OnRendar方法,它的定义是:
但我们不能直接调用OnRender方法,也不能直接创建DrawingContext实例,但可以利用 DrawingGroup.Open 和DrawingVisual.RenderOpen。
这里举两个例子:
(1)自定义绘制Canvas:
2)保存图片到文件:
最后附上这里的一段话(http://blogs.msdn.com/timothyc/archive/2006/06/16/634638.aspx),除加重点文字以桔色示凸出外, 以原样提供:
Adding seemingly simple tweaks (e.g., clipping, bitmap effects) to our scene causes us to fall back to software, and software rending in WPF is slower than GDI+ software rendering.
First, the WPF software rendering code is derived from the GDI+ codebase. There are certain limits to what can be accomplished in hardware, and we have to work around what the hardware vendors give us. As graphics hardware evolves, those limits are likely to become better over time. If at least some portion of your scene is rendered in hardware, the cost of rendering is already going to be faster than it was in GDI+. Finally, we shipped a tool at the PDC called ‘Perforator’ to help identify where software rendering occurs.
(注意桔色文字部分)