代码改变世界

WPF 2D绘图(2)Geometry

2010-11-23 10:10  Clingingboy  阅读(6936)  评论(0编辑  收藏  举报

 

Shape是对Geometry的一种封装,Shape本质上还是通过绘制Geometry的形状,然后以填充笔刷来呈现效果

image

如Rectangle

          <Path Stroke="Black" StrokeThickness="1" Fill="#CCCCFF">
            <Path.Data>
              <RectangleGeometry Rect="30,55 100 30" />
            </Path.Data>
          </Path>

 

<Rectangle Stroke="Black" StrokeThickness="1" Fill="#CCCCFF" Width="100" Height="30"></Rectangle>

这两者是等价的

Rectangle 是对RectangleGeometry 的封装实现,Rectangle 布局内部将会重写,所以封装了起始点

Geometry(没有笔刷的透明形状)本身无法呈现,必须放在Path容器中才可以

同样的其他的shape也对应Geometry,如果没有的话则以PathGeometry表示.

GeometryGroup

可以同时将多个Geometry放在一起成为一个新的图形

          <Path Stroke="Black" StrokeThickness="1" Fill="#CCCCFF">
            <Path.Data>
              <GeometryGroup FillRule="Nonzero">
                <LineGeometry StartPoint="10,10" EndPoint="50,30" />
                <EllipseGeometry Center="40,70" RadiusX="30" RadiusY="30" />              
                <RectangleGeometry Rect="30,55 100 30" />
              </GeometryGroup>
            </Path.Data>
          </Path>

 

好比Path是一张画布,代表着一个FrameworkElement。如果使用Shape的话,则需要三个FrameworkElement,这是对性能的挑战.FrameworkElement多的话将会大大降低程序的性能,这时还得采用传统的绘图方法.

即Shape依赖于Geometry

1000个Shape会产生1000个Geometry,但1000个Geometry可以放在1个Shape中,性能不言而喻了