WPF繪圖方式詳解
WPF工具作為一種新的開發(fā)工具,主要是用來幫助我們實(shí)現(xiàn)與圖形處理相關(guān)方面的操作。那么今天我們將會(huì)了解到有關(guān)WPF繪圖的相關(guān)方法。#t#
WPF繪圖提供了Ellipse等標(biāo)簽畫圖形,不僅如此而且還提供了許多的事件(因?yàn)槠淅^承自FrameworkElement).在某些情況下,我們可以不采用這些標(biāo)簽。
僅僅用于呈現(xiàn),并不復(fù)雜的操作(沒有事件)。
DrawingVisual是一個(gè)輕量繪圖類,在上述情況成立下可以采用DrawingVisual類來繪圖提高性能。
WPF繪圖方法如下
1.使用DrawingVisual必須創(chuàng)建一個(gè)容器(從FrameworkElement繼承創(chuàng)建一個(gè)容器)
- public class MyVisualHost
: FrameworkElement- {
- }
2.創(chuàng)建一個(gè)全局可視對(duì)象的集合(VisualCollection)
- private VisualColl
ection _children;- public MyVisualHost()
- {
- _children = new
VisualCollection(this);- }
3.創(chuàng)建DrawingVisual
先初始化一個(gè)DrawingVisual類,然后使用RenderOpen()獲取其DrawingContext 對(duì)象(DrawingContext 不可以以new方式初始化),DrawingContext 提供了一些以Draw開頭的繪圖方法,繪制完成以后必須調(diào)用Close方法(不然不會(huì)呈現(xiàn))
- private DrawingVisual
CreateDrawingVisualRectangle()- {
- DrawingVisual drawingVisual =
new DrawingVisual();- // Retrieve the DrawingContext
in order to create new drawing
content.- DrawingContext drawingContext =
drawingVisual.RenderOpen();- // Create a rectangle and
draw it in the DrawingContext.- Rect rect = new Rect(new
Point(160, 100), new Size(320, 80));- drawingContext.DrawRectangle
(Brushes.LightBlue, (Pen)null, rect);- // Persist the drawing content.
- drawingContext.Close();
- return drawingVisual;
- }
WPF繪圖DrawingContext還包含一些以Push開頭的方法,可以為圖形設(shè)置透明度,effect等??磗dk就ok了,記錄下
另外使用DrawingGroup也可以,不過容器變成了Image
- // Display the drawing
using an image control.- Image theImage = new Image();
- DrawingImage dImageSource =
new DrawingImage(dGroup);- theImage.Source = dImageSource;
- panel.Children.Add(theImage);
以上就是對(duì)WPF繪圖的相關(guān)方法的具體介紹。