. NET Basics: Vector graphics and WPF shape classes

Source: Internet
Author: User

Even in the relatively tedious field of two-dimensional vector graphics, windows®presentation Foundation (WPF) will still require programmers to learn many new concepts. In WPF, drawing objects have been elevated to almost equal status with controls, often participating in layouts and receiving mouse, keyboard, and stylus input. In addition, graphics systems retain these drawing objects so that they are no longer redrawn as frequently as they used to, and they can be moved and used as targets for data binding.

When I started studying WPF, I immediately thought of System.Windows.Shapes as a namespace containing the "Baby" graphics class. These classes seem to be suitable for displaying simple lines and rectangles, but I think a mature WPF program might want to implement a variety of functions by overriding the OnRender method and invoking the methods in the DrawingContext class.

The drawgeometry approach seems particularly tempting: in WPF, the Geometry object is a combination of connected and disconnected lines, arcs, and Bézier curves (referred to as "paths" in traditional graphics programming). The three parameters of the drawgeometry include the Geometry object, the Pen used to draw the Geometry line and curve, and the Brush used to populate the enclosing area.

The role of the Shapes name space

Soon, I found my first impression of WPF vector graphics to be wrong. Most WPF programs do not need to override the OnRender method and Invoke methods in the DrawingContext class. While rewriting OnRender is a good training exercise, it is often not necessary to rewrite it in most mainstream applications.

So, at least in my opinion, the System.Windows.Shapes namespace becomes a namespace for rendering two-dimensional vector graphics in WPF. The System.Windows.Shapes namespace contains the following classes: Shape (abstract class) and line, Polyline, Polygon, Path, Rectangle, and Ellipse (all encapsulated classes).

The Shape class itself derives from FrameworkElement. The most important Shape derived class is undoubtedly Path; This class has the same functionality as the DrawingContext Drawgeometry method, but it is much less troublesome. When you use the Path class in XAML, you can even define a Geometry object by using a coded drawing command string.

This does not mean that the Shapes class constructs a common vector graphics solution for all applications. Each instance of each class is a mature WPF element, and may incur more overhead. In addition, each class has only one brush and one fill brush, and may provide less color than you need.

To render complex vector graphics with multiple colors, there are a number of ways to choose. Of course, you can create multiple Path objects, but if you want to use a complex image as your own entity, this approach may be too complicated. At this point, a better solution is to use the DrawingGroup class, which can contain multiple GeometryDrawing objects, and each of these objects contains Geometry, brushes, and fill brushes. The DrawingGroup object may be the closest entity in WPF to a traditional graphic metafile. The DrawingGroup object can be used as the basis for the brush (through DrawingBrush), or it can be turned into a displayed Drawingimage object by the Image class.

Classes in the Shapes namespace are ideal if you only need an appropriate number of graphic primitives, especially if they need to receive a mouse, keyboard, or stylus input, or if they convert themselves.

Now, I'll introduce you to derive from the only unpackaged class Shape in the Shapes namespace. You can derive from the Shape class to implement a custom vector graphic primitive. Deriving from Shape is the easiest way to ensure that these custom primitives use the WPF Layout system protocol.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.